package com.jhhc.StormSurgeForecast.StormSurge.Uitl;

import com.alibaba.fastjson2.JSONObject;
import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_SENSORALARM_B;
import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_SENSOR_B;
import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_STBPRP_B;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSORALARM_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSOR_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_STBPRP_BService;
import com.jhhc.RHDB.Data.Pojo.Data;
import com.jhhc.RHDB.Data.Pojo.ST_TIDE_R;
import com.jhhc.RHDB.Data.Service.ST_TIDE_RService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_CALCULATESCHEME_M1;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_RESULT_P;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFINFO_B;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_CALCULATESCHEME_MService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_RESULT_PService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFINFO_BService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_ST_ASTROTD_FService;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.Dto.UpdateTideData;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.Dto.UpdateTideDataParam;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_OCEANMODEL_B;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_RESULT;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_RESULT_SCHEME;
import com.jhhc.StormSurgeForecast.StormSurge.Service.*;
import com.jhhc.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import java.util.stream.Collectors;


import static org.apache.commons.lang3.StringUtils.isWhitespace;

@Service
public class GetDataUtil {

    @Autowired
    private BDMS_FBC_RESULT_SCHEMEService bdms_fbc_result_schemeService;
    @Autowired
    private BDMS_FBC_RESULTService bdms_fbc_resultService;
    @Autowired
    private BDMS_ST_SENSOR_BService bdms_st_sensor_bService;
    @Autowired
    private ST_TIDE_RService st_tide_rService;
    @Autowired
    private BDMS_ST_ASTROTD_FService bdms_st_astrotd_fService;
    @Autowired
    private BDMS_ST_STBPRP_BService bdms_st_stbprp_bService;

    @Autowired
    private BDMS_ST_SENSORALARM_BService bdms_st_sensoralarm_bService;
    @Autowired
    private BDMS_FBC_OCEANMODEL_BService bdms_fbc_oceanmodel_bService;
    @Autowired
    private BDMS_CALCULATESCHEME_MService bdms_calculatescheme_mService;
    @Autowired
    private BDMS_FBC_RESULT_PService bdms_fbc_result_pService;
    @Autowired
    private ArithmeticUtil arithmeticUtil;

    @Autowired
    private BDMS_FBC_TFINFO_BService bdms_fbc_tfinfo_bService;
    // 日期格式转换
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar calendar = new GregorianCalendar();
    //数据格式化
    DecimalFormat df = new DecimalFormat("0.00");




    //生成数据  解析库数据 和预报时间构造增水数据
    List<Double> values;
    String data_all;
    //解析库数据方法
    public void data_all(String value) {
        if (values == null|| values.size()==0) {
            values = new ArrayList<>();
        } else {
            values.clear();
        }
        this.data_all = value;
        if (value == null) {
            return;
        }
        int version = 1;
        int pointIndex = value.indexOf(".");
        if (pointIndex > 0 && value.startsWith("v")) {
            version = Integer.parseInt(value.substring(1, pointIndex - 1));
        }
        if (version == 1) {
            int valueItemLength;
            //System.out.println("value.length():"+value.length());
            for (int i = 0; i < value.length(); i += valueItemLength) {
                // System.out.println("value.charAt(i):"+value.charAt(i));
                valueItemLength = 6;
                if (valueItemLength + i > value.length()) {
                    //System.out.println("tiaochu");
                    continue;
                } else {
                    if (!value.substring(i, valueItemLength + i).isEmpty()) {
                        //    System.out.println(i);
                        //  String str=value.substring(i, valueItemLength+i);
                        //    System.out.println("value.substring(i, valueItemLength)"+str);
                        double var = Double.parseDouble(value.substring(i, valueItemLength + i));
                        // System.out.println("var:"+var);
                        values.add(var / 100);
                    }

                }
            }
        } else if (version == 2) {
            String datavalue = value.substring(pointIndex + 1);
            String[] data0 = datavalue.split(String.valueOf("+"));
            for (int i = 0; i < data0.length; i++) {
                if (isWhitespace(data0[i])) {
                    continue;
                }
                String[] data1 = data0[i].split(String.valueOf("+"));
                for (int j = 0; j < data1.length; j++) {
                    if (isWhitespace(data1[j])) {
                        continue;
                    }
//                    if (j == 0) {
//                        values.add(Double.parseDouble(data1[0]));
//                    } else {
                    values.add(Double.parseDouble(data1[j]) * -1);
                    // }
                }
            }
        }
    }
    //返回增水数据
    public List<Data> getDataList(List<Double> values, String forecastTime,double update) {
        List<Data> dataList = new ArrayList<>();
        if (values != null) {
            for (int i = 0; i < values.size(); i++) {
                try {
                    Date date = format.parse(forecastTime);
                    calendar.setTime(date);
                    calendar.add(Calendar.HOUR, +i);
                    // dataList.add(new Data(format.format(calendar.getTime()), values.get(i)));
//                    if (values.get(i)<0){
//                        dataList.add(new Data(format.format(calendar.getTime()), 0.0));
//                    }else {
                    dataList.add(new Data(format.format(calendar.getTime()), values.get(i)+update));
                    //   }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            dataList.sort(Comparator.comparing(Data::getTM));
        }
        return dataList;
    }
    //获取实测数据方法
    public List<Data> getDataList(List<ST_TIDE_R> st_tide_rs) {
        List<Data> dataList = new ArrayList<>();
        for (ST_TIDE_R stTideR:st_tide_rs){
            Data data = new Data();
            data.setTM(stTideR.getTm().substring(0,19));
            data.setValue(Double.valueOf(df.format(stTideR.getTdz())));
            dataList.add(data);

        }
        return dataList;
    }
    public List<Data> getDataListStream(List<ST_TIDE_R> st_tide_rs) {
        return st_tide_rs.stream()
                .map(stTideR -> {
                    Data data = new Data();
                    data.setTM(stTideR.getTm().substring(0, 19));
                    data.setValue(Double.valueOf(df.format(stTideR.getTdz())));
                    return data;
                })
                .collect(Collectors.toList());
    }
    public CalResult getDataByCalSchemeIds(Integer pid, List<String> calSchemeIds, String stationCode, int twcDept, int backDays, Integer stepLength,Integer DBType) throws IOException, ParseException {
        CalResult calResult2 = new CalResult();
        List<String> stationCodes =new ArrayList<>();
        stationCodes.add(stationCode);
        List<CalSchemeInfo> calSchemeInfos = new ArrayList<>();
        List<ChartData> baseDatas = new ArrayList<>();

        double minV ;
        double maxV;
        //获取所有站点的警戒值
        List<BDMS_ST_SENSORALARM_B> bdms_st_sensoralarm_bs= bdms_st_sensoralarm_bService.selectByStcd2(stationCodes);
        //获取实测取数码
        List<String> Stcds = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b : bdms_st_sensor_bs) {
            Stcds.add(bdms_st_sensor_b.getDatacode());
        }
        BDMS_ST_SENSORALARM_B bdms_st_sensoralarmb= bdms_st_sensoralarm_bs.stream().filter(bdms_st_sensoralarm_b -> bdms_st_sensoralarm_b.getStcd()
                .equals(stationCode)).findAny().orElse(null);
        Double alarmValue = null;
        if (bdms_st_sensoralarmb != null) {
            alarmValue = bdms_st_sensoralarmb.getAlarmvalue();
        }
        String minStartTime = null;
        String maxEndTime = null;
        //当dbtype =0 huozhenull 老库查询
        if (DBType==1){
            int res =0;
            for (String calSchemeId : calSchemeIds) {

                //构造方案信息对象 包含返回结构
                CalSchemeInfo calSchemeInfo = new CalSchemeInfo();
                //结果对象
                List<ChartData> results = new ArrayList<>();
                res+=1;
                //获取计算方案
                List<BDMS_FBC_RESULT_SCHEME> bdms_fbc_result_schemes = bdms_fbc_result_schemeService.selectByPid(pid, Integer.parseInt(calSchemeId));
                BDMS_FBC_RESULT_SCHEME bdms_fbc_result_scheme = bdms_fbc_result_schemes.get(0);
                //设置方案基础信息
                calSchemeInfo.setCalSchemeId(String.valueOf(bdms_fbc_result_scheme.getSchemeId()));
                calSchemeInfo.setTyphoonId(String.valueOf(bdms_fbc_result_scheme.getTfid()));
                calSchemeInfo.setForecastTime(bdms_fbc_result_scheme.getYmdhm());
                try {
                    Date ForecastEndTM = format.parse(bdms_fbc_result_scheme.getYmdhm());
                    calendar.setTime(ForecastEndTM);
                    calendar.add(Calendar.HOUR,+bdms_fbc_result_scheme.getCalnum());
                    calSchemeInfo.setForecastEndTime(format.format(calendar.getTime()));
                }catch (Exception e){
                    e.printStackTrace();
                }
                calSchemeInfo.setUserId(String.valueOf(bdms_fbc_result_scheme.getUserId()));
                calSchemeInfo.setModelId(String.valueOf(bdms_fbc_result_scheme.getModelId()));
                calSchemeInfo.setTyphoonId(String.valueOf(bdms_fbc_result_scheme.getTfid()));
                calSchemeInfo.setTyphoonDepart(bdms_fbc_result_scheme.getReportDept());
                calSchemeInfo.setState(String.valueOf(bdms_fbc_result_scheme.getState()));
                calSchemeInfo.setModelPar(String.valueOf(bdms_fbc_result_scheme.getExpparam()));
                calSchemeInfo.setRemark(bdms_fbc_result_scheme.getFlag());
                calSchemeInfo.setRemark("state 0 新建方案、1 计算中、2 计算失败、3 计算成功");
                //获取单站方案计算结果
                List<BDMS_FBC_RESULT> bdms_fbc_results = bdms_fbc_resultService.selecTBySchemeId(Integer.parseInt(calSchemeId), stationCodes);
                //构造开始时间结束时间
                //获取预报时间
                String forecastTime = bdms_fbc_result_scheme.getYmdhm();
                calendar.setTime(format.parse(forecastTime));
                calendar.add(Calendar.DATE, -backDays);
                //预报时间点减去回退天数构造开始时间
                String startTime = format.format(calendar.getTime());
                //获取结束时间
                String endTime = calSchemeInfo.getForecastEndTime();
                //设置主副标题
                String stationName = null;
                BDMS_ST_SENSOR_B sensorB =  bdms_st_sensor_bService.selectbyStcd(stationCode);
                if (sensorB!=null){
                    stationName = sensorB.getStnm();
                }else {
                    stationName = "";
                }

                String mainTitle = stationName+"("+stationCode+")"+"潮位预报综合过程图";
                //当方案id大于1是只设置作业预报时间
                String subTitle;
                if (calSchemeIds.size()==1){
                    subTitle  = "作业预报时间："+forecastTime+" 预报路径："+calSchemeInfo.getTyphoonDepart()+" 预报模型："
                            +calSchemeInfo.getModelName()+" 台风系数："+calSchemeInfo.getModelPar();
                }else {
                    subTitle  = "作业预报时间："+forecastTime;
                }
                //构造xy轴
                String x = startTime + "," + endTime;
                String y = null;
                //警戒值
                calResult2.setStationCode(stationCode);
                calResult2.setStationName(stationName);
                calResult2.setTitle(mainTitle);
                calResult2.setAlarmValue(alarmValue);
                calResult2.setSubTitle(subTitle);
                calResult2.setAxisX(x);
                //获取预报前后所有的实测数据
                List<ST_TIDE_R> stTideRs = st_tide_rService.selectByStcd(Stcds,startTime,endTime);
                //解析实测
                String datacode = bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B -> BDMS_ST_SENSOR_B.getStcd().equals(stationCode)).findAny().orElse(null).getDatacode();
                List<ST_TIDE_R> st_tide_rs = stTideRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(datacode) && ST_TIDE_R.getMin().equals("00")).collect(Collectors.toList());
                //获取预报前的实测
                ArrayList<ST_TIDE_R> beforeTimeList = new ArrayList<>();
                //获取预报后的实测
                ArrayList<ST_TIDE_R> afterTimeList = new ArrayList<>();
                for (ST_TIDE_R obj : st_tide_rs) {
                    // 获取对象的时间点
                    Date objTime = format.parse(obj.getTm());
                    // 根据时间点与指定时间点进行比较
                    if (objTime.before(format.parse(forecastTime))) {
                        // 添加到指定时间点之前的集合
                        beforeTimeList.add(obj);
                    } else {
                        // 添加到指定时间点之后的集合
                        afterTimeList.add(obj);
                    }
                }
                //天文潮
                //获取天文潮
                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList = bdms_st_astrotd_fService.selectFTDZ(stationCodes,startTime,endTime,twcDept);
                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdmsStAstrotdFList.stream().filter(BDMS_ST_ASTROTD_F -> BDMS_ST_ASTROTD_F.getStcd().equals(stationCode)).collect(Collectors.toList());
                List<Data> datas = new ArrayList<>();
                //解析天文潮位
                for (BDMS_ST_ASTROTD_F bdmsStAstrotdF:bdmsStAstrotdFs ){
//                    if (bdmsStAstrotdF.getYmdh().substring(14,16).equals("00")){
                    Data data = new Data();
                    data.setTM(bdmsStAstrotdF.getYmdh().substring(0,19));
                    data.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz())));
                    datas.add(data);
                    //  }
                }
                //构造基础信息
                if (res==calSchemeIds.size()){
                    String[] baseValueNames = {"实测潮位-预报前","实测潮位-预报后","天文潮位"};
                    int k=0;
                    for (String valueName : baseValueNames) {
                        ChartData chartData = new ChartData();
                        List<Data> dataList;
                        switch (valueName) {
                            case "实测潮位-预报前":
                                dataList = getDataListStream(beforeTimeList);
                                if (dataList.size()>0){
                                    chartData.setSensorType("tdz");
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setGroupOrder(1);
                                    chartData.setOrder(1);
                                    chartData.setDatas(dataList);
                                    chartData.setValueName(valueName);
                                    chartData.setUnit("m");
                                    baseDatas.add(chartData);
                                }

                                break;
                            case "实测潮位-预报后":
                                chartData.setSensorType("tdz");
                                dataList = getDataListStream(afterTimeList);
                                if (dataList.size()>0) {
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setDatas(dataList);
                                    chartData.setGroupOrder(1);
                                    chartData.setOrder(2);
                                    chartData.setValueName(valueName);
                                    chartData.setUnit("m");
                                    baseDatas.add(chartData);
                                }
                                break;
                            case "天文潮位":
                                chartData.setSensorType("twc");
                                if (datas.size()>0){
                                    dataList = datas;
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setDatas(dataList);
                                    chartData.setGroupOrder(2);
                                    chartData.setOrder(1);
                                    chartData.setValueName(valueName);
                                    chartData.setUnit("m");
                                    baseDatas.add(chartData);
                                }

                                break;
                            default:
                                break;
                        }


                    }
                }
                baseDatas.sort(Comparator.comparing(ChartData::getGroupOrder)
                        .thenComparing(ChartData::getOrder));
                //calResult2.setBaseDatas(baseDatas);
                List<BDMS_FBC_RESULT> bdms_fbc_resultsList = bdms_fbc_results.stream().filter(BDMS_FBC_RESULT -> BDMS_FBC_RESULT.getStcd().equals(stationCode)).collect(Collectors.toList());



                // System.out.println("bdms_fbc_resultsList"+bdms_fbc_resultsList);
                if (bdms_fbc_resultsList.size()>0){
                    //获取值`xedcrtv fty
                    String[] valueNames = { "预报增水","预报潮位","特征潮位相应增水"};
                    //  List<BDMS_FBC_RESULT_P> bdms_fbc_resultsList = bdms_fbc_results.stream().filter(BDMS_FBC_RESULT -> BDMS_FBC_RESULT.getPtkey().equals(stationCode)).collect(Collectors.toList());
                    for (String valueName : valueNames) {
                        ChartData chartData = new ChartData();
                        List<Data> dataList ;
                        switch (valueName) {
                            case "预报增水":
                                chartData.setSensorType("zs");
                                dataList = new ArrayList<>();
                                for (BDMS_FBC_RESULT bdms_fbc_result : bdms_fbc_resultsList) {
                                    DATA_All(bdms_fbc_result.getDataAll());
                                }
                                //获取values
                                if (values != null) {
                                    for (int i = 0; i < values.size(); i++) {
                                        try {
                                            Date date = format.parse(forecastTime);
                                            calendar.setTime(date);
                                            calendar.add(Calendar.HOUR, +i);
                                            dataList.add(new Data(format.format(calendar.getTime()), values.get(i)));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                                dataList.sort(Comparator.comparing(Data::getTM));
                                chartData.setMaxData(dataList.stream()
                                        .max(Comparator.comparing(Data::getValue))
                                        .orElse(null));
                                chartData.setMinData(dataList.stream()
                                        .min(Comparator.comparing(Data::getValue))
                                        .orElse(null));
                                chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                chartData.setDatas(dataList);
                                chartData.setGroupOrder(3);
                                chartData.setOrder(1);
                                break;
                            case "预报潮位":
                                chartData.setSensorType("ftdz");
                                dataList = new ArrayList<>();
                                for (BDMS_FBC_RESULT bdms_fbc_result : bdms_fbc_resultsList) {
                                    DATA_All(bdms_fbc_result.getDataAll());
                                }
                                //获取values
                                if (values != null) {
                                    for (int i = 0; i < values.size(); i++) {
                                        try {
                                            Date date = format.parse(forecastTime);
                                            calendar.setTime(date);
                                            calendar.add(Calendar.HOUR, +i);
                                            dataList.add(new Data(format.format(calendar.getTime()), values.get(i)));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                                dataList.sort(Comparator.comparing(Data::getTM));
                                List<Data> dataList1 =new ArrayList<>();
                                for (Data data : getDataList(values, forecastTime,0)) {
                                    BDMS_ST_ASTROTD_F bdmsStAstrotdF = bdmsStAstrotdFs.stream().filter(bdms_st_astrotd_f ->
                                                    bdms_st_astrotd_f.getYmdh().substring(0, 19).
                                                            equals(data.getTM().substring(0, 19)))
                                            .findAny().orElse(null);
                                    if (bdmsStAstrotdF != null) {
                                        Data data1 = new Data();
                                        data1.setTM(data.getTM());
                                        data1.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz() + data.getValue())));
                                        dataList1.add(data1);
                                    }
                                }
                                if (dataList1.size()>0){
                                    chartData.setMaxData( dataList1.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData( dataList1.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    double maxValue = Double.parseDouble(df.format(chartData.getMaxData().getValue()));
                                    double minValue = Double.parseDouble(df.format(chartData.getMinData().getValue()));
                                    String y1 = null;
                                    if (chartData.getMaxData()!=null&&chartData.getMinData()!=null){
                                        maxValue = Double.parseDouble(df.format(chartData.getMaxData().getValue()));
                                        minValue = Double.parseDouble(df.format(chartData.getMinData().getValue()));
                                        if (minValue<0){

                                            y1 ="-"+Math.round(Math.abs(minValue)+0.5);
                                        }else {
                                            y1= String.valueOf(Math.round(minValue));
                                        }
                                        y= y1+","+Math.round((maxValue*1.5));
                                    }
                                }
                                calResult2.setAxisY(y);
                                chartData.setAverage(Double.valueOf(df.format(dataList1.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                chartData.setGroupOrder(3);
                                chartData.setOrder(2);
                                chartData.setDatas(dataList1);
                                break;
                            case "特征潮位相应增水":
                                chartData.setSensorType("zs");
                                for (BDMS_FBC_RESULT bdms_fbc_result : bdms_fbc_resultsList) {
                                    DATA_All(bdms_fbc_result.getDataAll());
                                }
                                dataList = new ArrayList<>();
                                for (BDMS_ST_ASTROTD_F bdmsStAstrotdF : bdmsStAstrotdFs) {
                                    Data data1 =  getDataList(values, forecastTime,0).stream().filter(data -> data.getTM().substring(0,19)
                                            .equals(bdmsStAstrotdF.getYmdh().substring(0,19))).findAny().orElse(null);
                                    if (data1==null){
                                        String old = bdmsStAstrotdF.getYmdh();
                                        Date newda = format.parse(bdmsStAstrotdF.getYmdh());
                                        int min = newda.getMinutes();
                                        newda.setMinutes(0);
                                        bdmsStAstrotdF.setYmdh(format.format(newda));
                                        Data prevData = null;
                                        Data nextData = null;
                                        for (int k = 0; k < getDataList(values, forecastTime,0).size(); k++) {
                                            if (getDataList(values, forecastTime,0).get(k).getTM().substring(0,19).equals(bdmsStAstrotdF.getYmdh().substring(0,19))) {
                                                if (k >= 0) {
                                                    prevData = getDataList(values, forecastTime,0).get(k);
                                                }
                                                if (k < getDataList(values, forecastTime,0).size() - 1) {
                                                    nextData = getDataList(values, forecastTime,0).get(k + 1);
                                                }
                                                break;
                                            }
                                        }
                                        if (prevData != null && nextData != null) {
                                            double averageValue = Double.parseDouble(df.format( prevData.getValue()+(( nextData.getValue()-prevData.getValue()) /60) *min));
                                            Data data2 = new Data();
                                            data2.setTM(old);
                                            data2.setHltdmk(bdmsStAstrotdF.getHltdmk());
                                            data2.setTwc(bdmsStAstrotdF.getFtdz());
                                            data2.setZs(averageValue);
                                            dataList.add(data2);
                                        }
                                    }
                                }
                                chartData.setGroupOrder(3);
                                chartData.setOrder(3);
                                chartData.setDatas(dataList);
                                break;
                            default:
                                break;
                        }
                        chartData.setValueName(valueName);
                        chartData.setUnit("m");
                        //datas 根据预报结果构造
                        results.add(chartData);
                    }
                }
                results.sort(Comparator.comparing(ChartData::getGroupOrder)
                        .thenComparing(ChartData::getOrder));
                results.removeIf(obj -> obj.getDatas().isEmpty());
                double maxValue1 = baseDatas.stream()
                        .flatMap(chartData -> chartData.getDatas().stream())
                        .filter(Objects::nonNull)
                        .mapToDouble(Data::getValue)
                        .max()
                        .orElse(Double.MIN_VALUE);
                double maxValue2 = results.stream()
                        .flatMap(chartData -> chartData.getDatas().stream())
                        .filter(data -> data != null && data.getValue() != null)
                        .mapToDouble(Data::getValue)
                        .max()
                        .orElse(Double.MIN_VALUE);
                double minValue1 = baseDatas.stream()
                        .flatMap(chartData -> chartData.getDatas().stream())
                        .filter(Objects::nonNull)
                        .mapToDouble(Data::getValue)
                        .min()
                        .orElse(Double.MIN_VALUE);

                double minValue2 = results.stream()
                        .flatMap(chartData -> chartData.getDatas().stream())
                        .filter(data -> data != null && data.getValue() != null)
                        .mapToDouble(Data::getValue)
                        .min()
                        .orElse(Double.MIN_VALUE);
                maxV = Math.max(maxValue1, maxValue2);
                minV = Math.min(minValue1, minValue2);

                String y1 = null;

                if (minV < 0) {

                    y1 = "-" + Math.round(Math.abs(minV) + 0.5);
                } else {
                    y1 = String.valueOf(Math.round(minV));
                }
                y = y1 + "," + Math.round((maxV * 1.5));
                calResult2.setAxisY(y);
                calSchemeInfo.setResults(results);
                baseDatas.removeIf(obj -> obj.getDatas().isEmpty());
                calResult2.setBaseDatas(baseDatas);
                calSchemeInfos.add(calSchemeInfo);
            }
            calResult2.setCalSchemeInfos(calSchemeInfos);
        } else if (DBType==null||DBType==0) {
            List<BDMS_CALCULATESCHEME_M1> bdms_fbc_result_schemes = bdms_calculatescheme_mService.selectShcemeByCalIds(calSchemeIds);
            // List<CalResult2> calResults = new ArrayList<>();
            //循环所有方案编号
            int res = 0;

            for (BDMS_CALCULATESCHEME_M1 bdmsCalculatescheme : bdms_fbc_result_schemes) {
                CalSchemeInfo calSchemeInfo = new CalSchemeInfo();
                //结果对象
                List<ChartData> results = new ArrayList<>();
                res+=1;
                //获取计算方案信息
                // BDMS_CALCULATESCHEME_M1 bdmsCalculatescheme = bdms_fbc_result_schemes.get(0);
                String calSchemeId=bdmsCalculatescheme.getCschemeid();
                //设置方案基础信息
                calSchemeInfo.setCalSchemeId(calSchemeId);
                calSchemeInfo.setCalSchemeName(bdmsCalculatescheme.getCschemenm());
                calSchemeInfo.setUserId(bdmsCalculatescheme.getUserid());
                calSchemeInfo.setUserName("预报员");
                calSchemeInfo.setForecastTime(bdmsCalculatescheme.getDtmforecast());
                calSchemeInfo.setForecastEndTime(bdmsCalculatescheme.getDtmend());
                calSchemeInfo.setForecastOperateTime(bdmsCalculatescheme.getDtmcalculate());
                String param = bdmsCalculatescheme.getParams();
                String[] params = param.split(",");
                String typhoonId = params[0];
                String dept= params[1];
                String xs =df.format(Double.parseDouble(params[2]));
                BDMS_FBC_TFINFO_B bdmsFbcTfinfoB = bdms_fbc_tfinfo_bService.queryById(String.valueOf(typhoonId));
                calSchemeInfo.setTyphoonId(typhoonId);
                calSchemeInfo.setTyphoonName(bdmsFbcTfinfoB.getTfnameC());
                calSchemeInfo.setModelPar(xs);
                calSchemeInfo.setTyphoonDepart(dept);
                BDMS_FBC_OCEANMODEL_B bdmsFbcOceanmodelB = bdms_fbc_oceanmodel_bService.selectByModelId(pid, bdmsCalculatescheme.getMdid());
                calSchemeInfo.setModelId(bdmsFbcOceanmodelB.getModelid());
                calSchemeInfo.setCalSchemeAbb(calSchemeInfo.getCalSchemeName()+" "+dept+" "+bdmsFbcOceanmodelB.getModelabb()+" "+xs);
                String modelName = bdmsFbcOceanmodelB.getModelnm();
                calSchemeInfo.setModelName(modelName);
                calSchemeInfo.setModelAbb(bdmsFbcOceanmodelB.getModelabb());
                calSchemeInfo.setFlag(bdmsCalculatescheme.getFlag());
                calSchemeInfo.setHasArea(bdmsCalculatescheme.getIsarea());
                calSchemeInfo.setHasGen(bdmsCalculatescheme.getIsgen());
                calSchemeInfo.setState(bdmsCalculatescheme.getState());
                calSchemeInfo.setRemark("state 0 新建方案、1 计算中、2 计算失败、3 计算成功");
                //获取单站方案计算结果
                List<BDMS_FBC_RESULT_P> bdms_fbc_results = bdms_fbc_result_pService.selecCSCHEMEID(calSchemeId);
                //获取更新数据
                List<UpdateTideData> updateTideDatas = null;
//               BDMS_FBC_RESULT_P bdms_fbc_result_p = bdms_fbc_results.stream().filter(x-> "updateData".equals(x.getPtkey())).findFirst().orElse(null);
//               if (bdms_fbc_result_p!= null) {
//                   JSONObject jsonObject = JSONObject.parseObject(bdms_fbc_result_p.getDtvalues());
//                   updateTideDatas = jsonObject.getList("updateTideDatas", UpdateTideData.class);
//               }
                //构造开始时间结束时
                calendar.setTime(format.parse(bdmsCalculatescheme.getDtmforecast()));
                calendar.add(Calendar.HOUR,+bdmsFbcOceanmodelB.getOutstartindexArea());
                String FORECAST = format.format(calendar.getTime());
                //获取预报时间
                String forecastTime = bdmsCalculatescheme.getDtmforecast();
                calendar.setTime(format.parse(forecastTime));
                calendar.add(Calendar.DATE, -backDays);
                //预报时间点减去回退天数构造开始时间
                String startTime = format.format(calendar.getTime());
                //获取结束时间
                String endTime = bdmsCalculatescheme.getDtmend();
                // 更新最小开始时间
                if (minStartTime == null || startTime.compareTo(minStartTime) < 0) {
                    minStartTime = startTime;
                }
                // 更新最大结束时间
                if (maxEndTime == null || endTime.compareTo(maxEndTime) > 0) {
                    maxEndTime = endTime;
                }

                //设置主副标题
                BDMS_ST_SENSOR_B bdms_st_sensor_b = bdms_st_sensor_bs.stream().filter(s -> s.getStcd().equals(stationCode)).findAny().orElse(null);
                if (bdms_st_sensor_b!=null){
                    String stationName = bdms_st_sensor_b.getStnm();
                    List<Data> updateDatas =null;
                    if (updateTideDatas!=null&&updateTideDatas.size()>0) {
                        UpdateTideData updateTideData = updateTideDatas.stream().filter(s -> s.getStationName().equals(stationName)).findAny().orElse(null);
                        //获取更新数据
                        updateDatas = updateTideData.getDatas();
                    }
                    String mainTitle = stationName+"("+stationCode+")"+"潮位预报综合过程图";
                    //当方案id大于1是只设置作业预报时间
                    String subTitle;
                    if (calSchemeIds.size()==1){
                        subTitle  = "作业预报时间："+forecastTime+" 预报路径："+dept+" 预报模型："
                                +modelName+" 台风系数："+xs;
                    }else {
                        subTitle  = "作业预报时间："+forecastTime;
                    }
                    //构造xy轴
                    String x = startTime + "," + endTime;
                    String y = "";
                    //警戒值
                    calResult2.setStationCode(stationCode);
                    calResult2.setStationName(stationName);
                    calResult2.setTitle(mainTitle);
                    calResult2.setAlarmValue(alarmValue);
                    calResult2.setSubTitle(subTitle);
                    calResult2.setAxisX(x);

                    List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList = bdms_st_astrotd_fService.selectFTDZ(stationCodes,minStartTime,maxEndTime,twcDept);
                    //天文潮
                    List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdmsStAstrotdFList.stream().filter(BDMS_ST_ASTROTD_F -> BDMS_ST_ASTROTD_F.getStcd().equals(stationCode)).collect(Collectors.toList());
                    //构造基础信息
                    Double max=null;
                    Double min =null;
                    if (res==calSchemeIds.size()){
                        //获取预报前后所有的实测数据
                        List<ST_TIDE_R> stTideRs = st_tide_rService.selectByStcd(Stcds,minStartTime,maxEndTime);
                        //解析实测
                        String datacode = bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B -> BDMS_ST_SENSOR_B.getStcd().equals(stationCode)).findAny().orElse(null).getDatacode();
                        List<ST_TIDE_R> st_tide_rs = stTideRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(datacode)).collect(Collectors.toList());
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        LocalDateTime compareTime = LocalDateTime.parse(forecastTime, formatter);
                        //System.out.println(format.format(new Date()));
                        List<ST_TIDE_R> beforeTimeList = st_tide_rs.stream()
                                .filter(time -> LocalDateTime.parse(time.getTm(), formatter).isBefore(compareTime))
                                .collect(Collectors.toList());
                        // System.out.println(format.format(new Date()));
                        List<ST_TIDE_R> afterTimeList = st_tide_rs.stream()
                                .filter(time -> LocalDateTime.parse(time.getTm(), formatter).isAfter(compareTime))
                                .collect(Collectors.toList());
                        List<Data> datas = new ArrayList<>();
                        //解析天文潮位
                        for (BDMS_ST_ASTROTD_F bdmsStAstrotdF:bdmsStAstrotdFs ){
                            Data data = new Data();
                            data.setTM(bdmsStAstrotdF.getYmdh().substring(0,19));
                            data.setValue(bdmsStAstrotdF.getFtdz());
                            if (data.getHltdmk()!=null && data.getHltdmk()>0){
                                data.setHltdmk(bdmsStAstrotdF.getHltdmk());
                            }
                            datas.add(data);
                        }
                        String[] baseValueNames = {"实测潮位-预报前","实测潮位-预报后","天文潮位"};
                        for (String valueName : baseValueNames) {
                            ChartData chartData = new ChartData();
                            List<Data> dataList;
                            switch (valueName) {
                                case "实测潮位-预报前":
                                    chartData.setSensorType("tdz");
                                    // System.out.println(format.format(new Date())+"1");
                                    dataList = getDataListStream(beforeTimeList);
                                    // System.out.println(format.format(new Date())+1);
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setGroupOrder(1);
                                    chartData.setOrder(1);
                                    chartData.setDatas(dataList);
                                    if (chartData.getMaxData()!=null){
                                        max =chartData.getMaxData().getValue();
                                    }
                                    if (chartData.getMinData()!=null){
                                        min =chartData.getMinData().getValue();
                                    }
                                    break;
                                case "实测潮位-预报后":
                                    chartData.setSensorType("tdz");
                                    // System.out.println(format.format(new Date())+"2");
                                    dataList = getDataListStream(afterTimeList);
                                    //  System.out.println(format.format(new Date())+"2");
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setDatas(dataList);
                                    chartData.setGroupOrder(1);
                                    chartData.setOrder(2);
                                    if (chartData.getMaxData()!=null){
                                        if (max==null){
                                            max =chartData.getMaxData().getValue();
                                        }else {
                                            if (max<chartData.getMaxData().getValue()){
                                                max =chartData.getMaxData().getValue();
                                            }
                                        }

                                    }
                                    if (chartData.getMinData()!=null){
                                        if (min==null){
                                            min =chartData.getMinData().getValue();
                                        }else {
                                            if (min>chartData.getMinData().getValue()){
                                                min =chartData.getMinData().getValue();
                                            }
                                        }
                                    }
                                    break;
                                case "天文潮位":
                                    chartData.setSensorType("twc");
                                    dataList = datas;
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setDatas(dataList);
                                    chartData.setGroupOrder(2);
                                    chartData.setOrder(1);
                                    if (chartData.getMaxData()!=null){
                                        if (max==null){
                                            max =chartData.getMaxData().getValue();
                                        }else {
                                            if (max<chartData.getMaxData().getValue()){
                                                max =chartData.getMaxData().getValue();
                                            }
                                        }

                                    }
                                    if (chartData.getMinData()!=null){
                                        if (min==null){
                                            min =chartData.getMinData().getValue();
                                        }else {
                                            if (min>chartData.getMinData().getValue()){
                                                min =chartData.getMinData().getValue();
                                            }
                                        }
                                    }
                                    break;
                                default:
                                    break;
                            }
                            chartData.setValueName(valueName);
                            chartData.setUnit("m");
                            baseDatas.add(chartData);
                        }
                    }
                    baseDatas.sort(Comparator.comparing(ChartData::getGroupOrder)
                            .thenComparing(ChartData::getOrder));

                    baseDatas = baseDatas.stream().filter(chartData -> chartData.getDatas().size()>0).collect(Collectors.toList());



                    List<BDMS_FBC_RESULT_P> bdms_fbc_resultsList = bdms_fbc_results.stream()
                            .filter(result -> result.getPtkey().equals(stationCode))
                            .collect(Collectors.toList());

                    double update = 0;
                    if (bdms_fbc_resultsList.size()>0){
                        for (BDMS_FBC_RESULT_P bdms_fbc_result : bdms_fbc_resultsList) {
                            if (bdms_fbc_result!=null){
                                if (bdms_fbc_result.getDttp()==2){
                                    data_all(bdms_fbc_result.getDtvalues());
                                }else if (bdms_fbc_result.getDttp()==4){
                                    data_all(bdms_fbc_result.getDtvalues());
                                }
                                if (bdms_fbc_result.getRemark()!=null) {
                                    update = Double.parseDouble(bdms_fbc_result.getRemark());
                                }
                            }

                        }
                    }else {
                        if (values!=null){
                            values.clear();
                        }
                    }


                    //判断modeltypeId
                    String[] valueNames = new String[0];
                    switch (bdmsFbcOceanmodelB.getModeltpid()){
                        case 2:
                            valueNames = new String[]{"预报增水", "预报潮位", "特征潮位相应增水"};
                            break;
                        case 4:
                            valueNames = new String[]{"预报潮位"};
                            break;
                        default:
                            break;
                    }
                    //String[] valueNames = {"预报增水", "预报潮位", "特征潮位相应增水"};
                    for (String valueName : valueNames) {
                        ChartData chartData = new ChartData();
                        chartData.setValueName(valueName);
                        chartData.setUnit("m");
                        chartData.setGroupOrder(3);
                        List<Data> dataList ;
                        switch (valueName) {
                            case "预报增水":
                                chartData.setSensorType("zs");
                                List<Data> dataListForecast = getDataList(values, FORECAST,update);
                                if (!dataListForecast.isEmpty()) {
                                    Data maxData = Collections.max(dataListForecast, Comparator.comparing(Data::getValue));
                                    Data minData = Collections.min(dataListForecast, Comparator.comparing(Data::getValue));
                                    double sum = dataListForecast.stream()
                                            .mapToDouble(Data::getValue)
                                            .sum();
                                    Double average = sum / dataListForecast.size();
                                    chartData.setDatas(dataListForecast);
                                    chartData.setMaxData(maxData);
                                    chartData.setMinData(minData);
                                    if (chartData.getMinData() != null) {
                                        if (min == null) {
                                            min = chartData.getMinData().getValue();
                                        } else {
                                            if (min > chartData.getMinData().getValue()) {
                                                min = chartData.getMinData().getValue();
                                            }
                                        }
                                    }
                                    chartData.setAverage(Double.valueOf(df.format(average)));
                                }
                                chartData.setOrder(1);
                                break;
                            case "预报潮位":
                                chartData.setSensorType("ftdz");
                                dataList = new ArrayList<>();


                                if (bdmsFbcOceanmodelB.getModeltpid() == 2) {
                                    for (BDMS_ST_ASTROTD_F bdmsStAstrotdF : bdmsStAstrotdFs) {
                                        dataListForecast = getDataList(values, FORECAST,update);
                                        Data data1 = dataListForecast.stream()
                                                .filter(data -> data.getTM().substring(0, 19)
                                                        .equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                                .findFirst()
                                                .orElse(null);
                                        if (data1 != null) {
                                            data1.setTM(data1.getTM());
                                            data1.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz() + data1.getValue())));
                                            dataList.add(data1);
                                        } else {
                                            String old = bdmsStAstrotdF.getYmdh();
                                            Date newda = format.parse(bdmsStAstrotdF.getYmdh());
                                            int minutes = newda.getMinutes();
                                            newda.setMinutes(0);
                                            bdmsStAstrotdF.setYmdh(format.format(newda));
                                            Optional<Data> prevDataOptional = dataListForecast.stream()
                                                    .filter(data -> data.getTM().substring(0, 19).equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                                    .findFirst();
                                            if (prevDataOptional.isPresent()) {
                                                calendar.setTime(format.parse(prevDataOptional.get().getTM()));
                                                calendar.add(Calendar.HOUR, +1);
                                                Optional<Data> nextDataOptional = dataListForecast.stream()
                                                        .filter(data -> data.getTM().substring(0, 19).equals(format.format(calendar.getTime())))
                                                        .findFirst();
                                                prevDataOptional.ifPresent(prevData -> {
                                                    nextDataOptional.ifPresent(nextData -> {
                                                        double averageValue = Double.parseDouble(df.format(prevData.getValue() + ((nextData.getValue() - prevData.getValue()) / 60) * minutes));
                                                        newda.setMinutes(minutes);
                                                        bdmsStAstrotdF.setYmdh(format.format(newda));
                                                        Data data2 = new Data();
                                                        data2.setTM(old);
                                                        data2.setHltdmk(bdmsStAstrotdF.getHltdmk());
                                                        data2.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz() + averageValue)));
                                                        dataList.add(data2);
                                                    });
                                                });
                                                bdmsStAstrotdF.setYmdh(old);
                                            }
                                        }
                                    }
                                    if (!dataList.isEmpty()) {
                                        chartData.setMaxData(dataList.stream()
                                                .max(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setMinData(dataList.stream()
                                                .min(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        if (chartData.getMaxData() != null) {
                                            if (max == null) {
                                                max = chartData.getMaxData().getValue();
                                            } else {
                                                if (max < chartData.getMaxData().getValue()) {
                                                    max = chartData.getMaxData().getValue();
                                                }
                                            }
                                        }
                                        if (chartData.getMinData() != null) {
                                            if (min == null) {
                                                min = chartData.getMinData().getValue();
                                            } else {
                                                if (min > chartData.getMinData().getValue()) {
                                                    min = chartData.getMinData().getValue();
                                                }
                                            }
                                        }
                                        String y1 = null;
                                        if (chartData.getMaxData() != null && chartData.getMinData() != null) {
                                            if (min < 0) {
                                                y1 = "-" + Math.round(Math.abs(min) + 0.5);
                                            } else {
                                                y1 = String.valueOf(Math.round(min));
                                            }
                                            y = y1 + "," + Math.round((max * 1.5));
                                        }
                                    }
                                    calResult2.setAxisY(y);
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setOrder(2);
                                    //循环updateDatas
                                    if (updateDatas!=null) {
                                        for (Data updateData : updateDatas) {
                                            System.out.println(updateData);
                                            String tm = updateData.getTM();
                                            dataList.stream().filter(data -> data.getTM().equals(tm)).findFirst().get().setValue(updateData.getValue());
                                        }
                                    }
                                    chartData.setDatas(dataList);
                                } else if (bdmsFbcOceanmodelB.getModeltpid() == 4) {
                                    dataListForecast = getDataList(values, FORECAST,update);
                                    if (!dataListForecast.isEmpty()) {
                                        Data maxData = Collections.max(dataListForecast, Comparator.comparing(Data::getValue));
                                        Data minData = Collections.min(dataListForecast, Comparator.comparing(Data::getValue));
                                        double sum = dataListForecast.stream()
                                                .mapToDouble(Data::getValue)
                                                .sum();
                                        Double average = sum / dataListForecast.size();
                                        chartData.setDatas(dataListForecast);
                                        chartData.setMaxData(maxData);
                                        chartData.setMinData(minData);
                                        chartData.setAverage(Double.valueOf(df.format(average)));
                                        chartData.setOrder(1);
                                        if (chartData.getMaxData() != null) {
                                            if (max == null) {
                                                max = chartData.getMaxData().getValue();
                                            } else {
                                                if (max < chartData.getMaxData().getValue()) {
                                                    max = chartData.getMaxData().getValue();
                                                }
                                            }
                                        }
                                        if (chartData.getMinData() != null) {
                                            if (min == null) {
                                                min = chartData.getMinData().getValue();
                                            } else {
                                                if (min > chartData.getMinData().getValue()) {
                                                    min = chartData.getMinData().getValue();
                                                }
                                            }
                                        }
                                        String y1;
                                        if (chartData.getMaxData() != null && chartData.getMinData() != null) {
                                            if (min < 0) {
                                                y1 = "-" + Math.round(Math.abs(min) + 0.5);
                                            } else {
                                                y1 = String.valueOf(Math.round(min));
                                            }
                                            y = y1 + "," + Math.round((max * 1.5));
                                            calResult2.setAxisY(y);
                                        }
                                    }
                                }
                                break;
                            case "特征潮位相应增水":
                                chartData.setSensorType("tzzs");
                                List<Data> dataListFeature = new ArrayList<>();
                                for (BDMS_ST_ASTROTD_F bdmsStAstrotdF : bdmsStAstrotdFs) {

                                    dataListForecast = getDataList(values, FORECAST,update);
                                    Data data1 = dataListForecast.stream()
                                            .filter(data -> data.getTM().substring(0, 19)
                                                    .equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                            .findFirst()
                                            .orElse(null);
                                    if (data1 == null) {
                                        String old = bdmsStAstrotdF.getYmdh();
                                        Date newda = format.parse(bdmsStAstrotdF.getYmdh());
                                        int minutes = newda.getMinutes();
                                        newda.setMinutes(0);
                                        bdmsStAstrotdF.setYmdh(format.format(newda));

                                        Optional<Data> prevDataOptional = dataListForecast.stream()
                                                .filter(data -> data.getTM().substring(0, 19).equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                                .findFirst();
                                        // System.out.println(prevDataOptional);
                                        if (prevDataOptional.isPresent()) {
                                            calendar.setTime(format.parse(prevDataOptional.get().getTM()));
                                            calendar.add(Calendar.HOUR, +1);
                                            // System.out.println(format.format(calendar.getTime()));
                                            Optional<Data> nextDataOptional = dataListForecast.stream()
                                                    .filter(data -> data.getTM().substring(0, 19).equals(format.format(calendar.getTime())))
                                                    .findFirst();
                                            // System.out.println(nextDataOptional);
                                            prevDataOptional.ifPresent(prevData -> {
                                                nextDataOptional.ifPresent(nextData -> {

                                                    double averageValue = Double.parseDouble(df.format(prevData.getValue() + ((nextData.getValue() - prevData.getValue()) / 60) * minutes));
                                                    Data data2 = new Data();
                                                    data2.setTM(old);
                                                    data2.setHltdmk(bdmsStAstrotdF.getHltdmk());
                                                    data2.setTwc(bdmsStAstrotdF.getFtdz());
                                                    data2.setZs(averageValue);
                                                    dataListFeature.add(data2);
                                                });
                                            });
                                            bdmsStAstrotdF.setYmdh(old);
                                        }
                                    }
                                }
                                chartData.setOrder(3);
                                chartData.setDatas(dataListFeature);
                                break;
                            default:
                                break;
                        }
                        results.add(chartData);
                    }
                    if (results.size()>2) {
                        List<Data>  dataList =results.get(2).getDatas().stream().filter(data -> data.getZs()!=null).collect(Collectors.toList());
                        for (Data data : dataList) {
                            data.setValue(data.getZs());
                            data.setTwc(null);
                            data.setZs(null);
                            results.get(0).getDatas().add(data);
                        }


                        if (results.get(0).getDatas()!=null&&results.get(0).getDatas().size()>0){
                            results.get(0).getDatas().sort(Comparator.comparing(Data::getTM));
                        }
                        if (results.get(1).getMaxData()!=null){
                            if (max==null){
                                max =results.get(1).getMaxData().getValue();
                            }else {
                                if (max<results.get(1).getMaxData().getValue()){
                                    max =results.get(1).getMaxData().getValue();
                                }
                            }

                        }
                        if (results.get(1).getMinData()!=null){
                            if (min==null){
                                min =results.get(1).getMinData().getValue();
                            }else {
                                if (min>results.get(1).getMinData().getValue()){
                                    min =results.get(1).getMinData().getValue();
                                }
                            }
                        }
                        String y1;
                        if (results.get(1).getMaxData()!=null&&results.get(1).getMinData()!=null){

                            if (min<0){

                                y1 ="-"+Math.round(Math.abs(min)+0.5);
                            }else {
                                y1= String.valueOf(Math.abs(min)-0.5);

                            }
                            y= y1+","+Math.round((max*1.5));

                            calResult2.setAxisY(y);
                        }

                    }

                    results.sort(Comparator.comparing(ChartData::getGroupOrder)
                            .thenComparing(ChartData::getOrder));
                    results = results.stream()
                            .filter(chartData -> chartData.getDatas() != null && chartData.getDatas().size() > 0)
                            .collect(Collectors.toList());
                    calSchemeInfo.setResults(results);
                    String y1;
                    if (max != null && min != null) {
                        if (min < 0) {
                            y1 = "-" + Math.round(Math.abs(min) + 0.5);
                        } else {
                            y1 = String.valueOf(Math.round(min-0.5));
                        }
                        System.out.println(y1);
                        System.out.println(String.format("%.2f",max * 1.5));
                        if (calResult2.getAlarmValue()!=null) {
                            System.out.println(String.format("%.2f", calResult2.getAlarmValue()));

                            if (max * 1.5 > calResult2.getAlarmValue()) {
                                max = max + 1.0;
                            }
                        }
//                        if (Math.round((max * 1.5).equals(calResult2.getAlarmValue())){
//
//                        }
                        y = y1 + "," + Math.round((max * 1.5));
                        calResult2.setAxisY(y);
                    }else {
                        calResult2.setAxisY("-2,6");
                    }
                    calResult2.setBaseDatas(baseDatas);
                    calSchemeInfos.add(calSchemeInfo);
                    calResult2.setCalSchemeInfos(calSchemeInfos);
                }
            }
        }




        return calResult2;
    }




    public List<CalResult> getDataByCalSchemeIds3(Integer pid, String calSchemeId, List<String> stationCodes, int twcDept, int backDays) throws ParseException {

        //获取所有站点的警戒值
        List<BDMS_ST_SENSORALARM_B> bdms_st_sensoralarm_bs= bdms_st_sensoralarm_bService.selectByStcd2(stationCodes);
        //获取实测取数码
        List<String> Stcds = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b : bdms_st_sensor_bs) {
            Stcds.add(bdms_st_sensor_b.getDatacode());
        }


        BDMS_CALCULATESCHEME_M1 bdmsCalculatescheme = bdms_calculatescheme_mService.selcalSchemeId(calSchemeId);
        List<CalResult> calResults = new ArrayList<>();

        BDMS_FBC_OCEANMODEL_B bdmsFbcOceanmodelB = bdms_fbc_oceanmodel_bService.selectByModelId(pid, bdmsCalculatescheme.getMdid());
        CalSchemeInfo calSchemeInfo = new CalSchemeInfo();
        //设置方案基础信息
        calSchemeInfo.setCalSchemeId(calSchemeId);
        calSchemeInfo.setCalSchemeName(bdmsCalculatescheme.getCschemenm());
        calSchemeInfo.setUserId(bdmsCalculatescheme.getUserid());
        calSchemeInfo.setUserName("预报员");
        calSchemeInfo.setForecastTime(bdmsCalculatescheme.getDtmforecast());
        calSchemeInfo.setForecastEndTime(bdmsCalculatescheme.getDtmend());
        calSchemeInfo.setForecastOperateTime(bdmsCalculatescheme.getDtmcalculate());
        String param = bdmsCalculatescheme.getParams();
        String[] params = param.split(",");
        String typhoonId = params[0];
        String dept= params[1];
        String xs =df.format(Double.parseDouble(params[2]));
        BDMS_FBC_TFINFO_B bdmsFbcTfinfoB = bdms_fbc_tfinfo_bService.queryById(String.valueOf(typhoonId));
        calSchemeInfo.setTyphoonId(typhoonId);
        calSchemeInfo.setTyphoonName(bdmsFbcTfinfoB.getTfnameC());
        calSchemeInfo.setModelPar(xs);
        calSchemeInfo.setTyphoonDepart(dept);

        calSchemeInfo.setModelId(bdmsFbcOceanmodelB.getModelid());
        calSchemeInfo.setCalSchemeAbb(calSchemeInfo.getCalSchemeName()+" "+dept+" "+bdmsFbcOceanmodelB.getModelabb()+" "+xs);
        String modelName = bdmsFbcOceanmodelB.getModelnm();
        calSchemeInfo.setModelName(modelName);
        calSchemeInfo.setModelAbb(bdmsFbcOceanmodelB.getModelabb());
        calSchemeInfo.setFlag(bdmsCalculatescheme.getFlag());
        calSchemeInfo.setHasArea(bdmsCalculatescheme.getIsarea());
        calSchemeInfo.setHasGen(bdmsCalculatescheme.getIsgen());
        calSchemeInfo.setState(bdmsCalculatescheme.getState());
        calSchemeInfo.setRemark("state 0 新建方案、1 计算中、2 计算失败、3 计算成功");
        //获取单站方案计算结果
        List<BDMS_FBC_RESULT_P> bdms_fbc_results = bdms_fbc_result_pService.selecCSCHEMEID(calSchemeId);
        //获取更新数据
        List<UpdateTideData> updateTideDatas = null;
        BDMS_FBC_RESULT_P bdms_fbc_result_p = bdms_fbc_results.stream().filter(x-> "updateData".equals(x.getPtkey())).findFirst().orElse(null);
        if (bdms_fbc_result_p!= null) {
            JSONObject jsonObject = JSONObject.parseObject(bdms_fbc_result_p.getDtvalues());
            updateTideDatas = jsonObject.getList("updateTideDatas", UpdateTideData.class);
        }
        //构造开始时间结束时
        calendar.setTime(format.parse(bdmsCalculatescheme.getDtmforecast()));
        calendar.add(Calendar.HOUR,+bdmsFbcOceanmodelB.getOutstartindexArea());
        String FORECAST = format.format(calendar.getTime());
        //获取预报时间
        String forecastTime = bdmsCalculatescheme.getDtmforecast();
        calendar.setTime(format.parse(forecastTime));
        calendar.add(Calendar.DATE, -backDays);
        //预报时间点减去回退天数构造开始时间
        String startTime = format.format(calendar.getTime());
        //获取结束时间
        String endTime = bdmsCalculatescheme.getDtmend();
        //循环站点
        String x = startTime + "," + endTime;
        //获取所有站点的天文潮数据
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList = bdms_st_astrotd_fService.selectFTDZ(stationCodes,startTime,endTime,twcDept);
        //获取所有站点的实测潮位数据
        List<ST_TIDE_R> stTideRs = st_tide_rService.selectByStcd(Stcds,startTime,endTime);
        for (String stationCode : stationCodes) {


            //结果对象
            List<ChartData> results = new ArrayList<>();


            //设置主副标题
            //使用stationCode去和bdms_st_sensor_bs集合中BDMS_ST_SENSOR_B的getStnm去判断获取对象
            BDMS_ST_SENSOR_B bdms_st_sensor_b = bdms_st_sensor_bs.stream().filter(bdmsStSensorB -> bdmsStSensorB.getStcd().equals(stationCode)).findAny().orElse(null);
            if (bdms_st_sensor_b != null) {
                CalResult calResult2 = new CalResult();
                calResult2.setStationCode(stationCode);
                calResult2.setCalSchemeInfo(calSchemeInfo);
                List<ChartData> baseDatas = new ArrayList<>();
                String stationName = bdms_st_sensor_b.getStnm();
                List<Data> updateDatas =null;
                if (updateTideDatas!=null&&updateTideDatas.size()>0) {
                    UpdateTideData updateTideData = updateTideDatas.stream().filter(s -> s.getStationName().equals(stationName)).findAny().orElse(null);
                    //获取更新数据
                    updateDatas = updateTideData.getDatas();
                }
                calResult2.setStationName(stationName);
                String mainTitle = stationName + "(" + stationCode + ")" + "潮位预报综合过程图";
                //当方案id大于1是只设置作业预报时间
                String subTitle;
                subTitle = "作业预报时间：" + forecastTime.substring(0,16) + " 预报路径：" + dept + " 预报模型："
                        + modelName + " 台风系数：" + xs;
                calResult2.setTitle(mainTitle);
                calResult2.setSubTitle(subTitle);
                calResult2.setAxisX(x);
                String y = "";
                BDMS_ST_SENSORALARM_B bdms_st_sensoralarmb= bdms_st_sensoralarm_bs.stream().filter(bdms_st_sensoralarm_b -> bdms_st_sensoralarm_b.getStcd()
                        .equals(stationCode)).findAny().orElse(null);
                Double alarmValue = null;
                if (bdms_st_sensoralarmb != null) {
                    alarmValue = bdms_st_sensoralarmb.getAlarmvalue();
                }
                calResult2.setAlarmValue(alarmValue);
                //获取数据
                //天文潮
                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdmsStAstrotdFList.stream().filter(BDMS_ST_ASTROTD_F -> BDMS_ST_ASTROTD_F.getStcd().equals(stationCode)).collect(Collectors.toList());
                //构造基础信息
                Double max=null;
                Double min =null;
                //解析实测
                String datacode = bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B -> BDMS_ST_SENSOR_B.getStcd().equals(stationCode)).findAny().orElse(null).getDatacode();
                List<ST_TIDE_R> st_tide_rs = stTideRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(datacode)).collect(Collectors.toList());
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime compareTime = LocalDateTime.parse(forecastTime, formatter);
                //System.out.println(format.format(new Date()));
                List<ST_TIDE_R> beforeTimeList = st_tide_rs.stream()
                        .filter(time -> LocalDateTime.parse(time.getTm(), formatter).isBefore(compareTime))
                        .collect(Collectors.toList());
                // System.out.println(format.format(new Date()));
                List<ST_TIDE_R> afterTimeList = st_tide_rs.stream()
                        .filter(time -> LocalDateTime.parse(time.getTm(), formatter).isAfter(compareTime))
                        .collect(Collectors.toList());
                List<Data> datas = new ArrayList<>();
                //解析天文潮位
                for (BDMS_ST_ASTROTD_F bdmsStAstrotdF:bdmsStAstrotdFs ){
                    Data data = new Data();
                    data.setTM(bdmsStAstrotdF.getYmdh().substring(0,19));
                    data.setValue(bdmsStAstrotdF.getFtdz());
                    datas.add(data);
                }
                String[] baseValueNames = {"实测潮位-预报前","实测潮位-预报后","天文潮位"};
                for (String valueName : baseValueNames) {
                    ChartData chartData = new ChartData();
                    List<Data> dataList;
                    switch (valueName) {
                        case "实测潮位-预报前":
                            chartData.setSensorType("tdz");
                            // System.out.println(format.format(new Date())+"1");
                            dataList = getDataListStream(beforeTimeList);
                            // System.out.println(format.format(new Date())+1);
                            chartData.setMaxData(dataList.stream()
                                    .max(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            chartData.setMinData(dataList.stream()
                                    .min(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                            chartData.setGroupOrder(1);
                            chartData.setOrder(1);
                            chartData.setDatas(dataList);
                            if (chartData.getMaxData()!=null){
                                max =chartData.getMaxData().getValue();
                            }
                            if (chartData.getMinData()!=null){
                                min =chartData.getMinData().getValue();
                            }
                            break;
                        case "实测潮位-预报后":
                            chartData.setSensorType("tdz");
                            // System.out.println(format.format(new Date())+"2");
                            dataList = getDataListStream(afterTimeList);
                            //  System.out.println(format.format(new Date())+"2");
                            chartData.setMaxData(dataList.stream()
                                    .max(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            chartData.setMinData(dataList.stream()
                                    .min(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                            chartData.setDatas(dataList);
                            chartData.setGroupOrder(1);
                            chartData.setOrder(2);
                            if (chartData.getMaxData()!=null){
                                if (max==null){
                                    max =chartData.getMaxData().getValue();
                                }else {
                                    if (max<chartData.getMaxData().getValue()){
                                        max =chartData.getMaxData().getValue();
                                    }
                                }

                            }
                            if (chartData.getMinData()!=null){
                                if (min==null){
                                    min =chartData.getMinData().getValue();
                                }else {
                                    if (min>chartData.getMinData().getValue()){
                                        min =chartData.getMinData().getValue();
                                    }
                                }
                            }
                            break;
                        case "天文潮位":
                            chartData.setSensorType("twc");
                            dataList = datas;
                            chartData.setMaxData(dataList.stream()
                                    .max(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            chartData.setMinData(dataList.stream()
                                    .min(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                            chartData.setDatas(dataList);
                            chartData.setGroupOrder(2);
                            chartData.setOrder(1);
                            if (chartData.getMaxData()!=null){
                                if (max==null){
                                    max =chartData.getMaxData().getValue();
                                }else {
                                    if (max<chartData.getMaxData().getValue()){
                                        max =chartData.getMaxData().getValue();
                                    }
                                }

                            }
                            if (chartData.getMinData()!=null){
                                if (min==null){
                                    min =chartData.getMinData().getValue();
                                }else {
                                    if (min>chartData.getMinData().getValue()){
                                        min =chartData.getMinData().getValue();
                                    }
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    chartData.setValueName(valueName);
                    chartData.setUnit("m");
                    baseDatas.add(chartData);
                }
                baseDatas.sort(Comparator.comparing(ChartData::getGroupOrder)
                        .thenComparing(ChartData::getOrder));

                baseDatas = baseDatas.stream().filter(chartData -> chartData.getDatas().size()>0).collect(Collectors.toList());
                List<BDMS_FBC_RESULT_P> bdms_fbc_resultsList = bdms_fbc_results.stream()
                        .filter(result -> result.getPtkey().equals(stationCode))
                        .collect(Collectors.toList());

                double update =0;
                if (bdms_fbc_resultsList.size()>0){
                    for (BDMS_FBC_RESULT_P bdms_fbc_result : bdms_fbc_resultsList) {
                        if (bdms_fbc_result!=null){
                            if (bdms_fbc_result.getDttp()==2){
                                data_all(bdms_fbc_result.getDtvalues());
                            }else if (bdms_fbc_result.getDttp()==4){
                                data_all(bdms_fbc_result.getDtvalues());
                            }
                            if (bdms_fbc_result.getRemark()!=null && !"".equals(bdms_fbc_result.getRemark())){
                                update = Double.parseDouble(bdms_fbc_result.getRemark());
                            }
                        }
                    }
                }else {
                    if (values!=null){
                        values.clear();
                    }
                }
                //判断modeltypeId
                String[] valueNames = new String[0];
                switch (bdmsFbcOceanmodelB.getModeltpid()){
                    case 2:
                        valueNames = new String[]{"预报增水", "预报潮位", "特征潮位相应增水"};
                        break;
                    case 4:
                        valueNames = new String[]{"预报潮位"};
                        break;
                    default:
                        break;
                }
                for (String valueName : valueNames) {
                    ChartData chartData = new ChartData();
                    chartData.setValueName(valueName);
                    chartData.setUnit("m");
                    chartData.setGroupOrder(3);
                    List<Data> dataList ;
                    switch (valueName) {
                        case "预报增水":
                            chartData.setSensorType("zs");
                            List<Data> dataListForecast = getDataList(values, FORECAST,update);
                            if (!dataListForecast.isEmpty()) {
                                Data maxData = Collections.max(dataListForecast, Comparator.comparing(Data::getValue));
                                Data minData = Collections.min(dataListForecast, Comparator.comparing(Data::getValue));
                                double sum = dataListForecast.stream()
                                        .mapToDouble(Data::getValue)
                                        .sum();
                                Double average = sum / dataListForecast.size();
                                chartData.setDatas(dataListForecast);
                                chartData.setMaxData(maxData);
                                chartData.setMinData(minData);
                                if (chartData.getMinData() != null) {
                                    if (min == null) {
                                        min = chartData.getMinData().getValue();
                                    } else {
                                        if (min > chartData.getMinData().getValue()) {
                                            min = chartData.getMinData().getValue();
                                        }
                                    }
                                }
                                chartData.setAverage(Double.valueOf(df.format(average)));
                            }
                            chartData.setOrder(1);
                            break;
                        case "预报潮位":
                            chartData.setSensorType("ftdz");
                            dataList = new ArrayList<>();
                            if (bdmsFbcOceanmodelB.getModeltpid() == 2) {
                                for (BDMS_ST_ASTROTD_F bdmsStAstrotdF : bdmsStAstrotdFs) {
                                    dataListForecast = getDataList(values, FORECAST,update);
                                    Data data1 = dataListForecast.stream()
                                            .filter(data -> data.getTM().substring(0, 19)
                                                    .equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                            .findFirst()
                                            .orElse(null);
                                    if (data1 != null) {
                                        data1.setTM(data1.getTM());
                                        data1.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz() + data1.getValue())));
                                        dataList.add(data1);
                                    } else {
                                        String old = bdmsStAstrotdF.getYmdh();
                                        Date newda = format.parse(bdmsStAstrotdF.getYmdh());
                                        int minutes = newda.getMinutes();
                                        newda.setMinutes(0);
                                        bdmsStAstrotdF.setYmdh(format.format(newda));
                                        Optional<Data> prevDataOptional = dataListForecast.stream()
                                                .filter(data -> data.getTM().substring(0, 19).equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                                .findFirst();
                                        if (prevDataOptional.isPresent()) {
                                            calendar.setTime(format.parse(prevDataOptional.get().getTM()));
                                            calendar.add(Calendar.HOUR, +1);
                                            Optional<Data> nextDataOptional = dataListForecast.stream()
                                                    .filter(data -> data.getTM().substring(0, 19).equals(format.format(calendar.getTime())))
                                                    .findFirst();
                                            prevDataOptional.ifPresent(prevData -> {
                                                nextDataOptional.ifPresent(nextData -> {
                                                    double averageValue = Double.parseDouble(df.format(prevData.getValue() + ((nextData.getValue() - prevData.getValue()) / 60) * minutes));
                                                    newda.setMinutes(minutes);
                                                    bdmsStAstrotdF.setYmdh(format.format(newda));
                                                    Data data2 = new Data();
                                                    data2.setTM(old);
                                                    data2.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz() + averageValue)));
                                                    dataList.add(data2);
                                                });
                                            });
                                            bdmsStAstrotdF.setYmdh(old);
                                        }
                                    }
                                }
                                if (!dataList.isEmpty()) {
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    if (chartData.getMaxData() != null) {
                                        if (max == null) {
                                            max = chartData.getMaxData().getValue();
                                        } else {
                                            if (max < chartData.getMaxData().getValue()) {
                                                max = chartData.getMaxData().getValue();
                                            }
                                        }
                                    }
                                    if (chartData.getMinData() != null) {
                                        if (min == null) {
                                            min = chartData.getMinData().getValue();
                                        } else {
                                            if (min > chartData.getMinData().getValue()) {
                                                min = chartData.getMinData().getValue();
                                            }
                                        }
                                    }
                                    String y1 = null;
                                    if (chartData.getMaxData() != null && chartData.getMinData() != null) {
                                        if (min < 0) {
                                            y1 = "-" + Math.round(Math.abs(min) + 0.5);
                                        } else {
                                            y1 = String.valueOf(Math.round(min));
                                        }
                                        y = y1 + "," + Math.round((max * 1.5));
                                    }
                                }
                                calResult2.setAxisY(y);
                                chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                chartData.setOrder(2);
                                //循环updateDatas
                                //循环updateDatas
                                if (updateDatas!=null) {
                                    for (Data updateData : updateDatas) {
                                        String tm = updateData.getTM();
                                        dataList.stream().filter(data -> data.getTM().equals(tm)).findFirst().get().setValue(updateData.getValue());
                                    }
                                }
                                chartData.setDatas(dataList);
                            } else if (bdmsFbcOceanmodelB.getModeltpid() == 4) {
                                dataListForecast =getDataList(values, FORECAST,update);
                                if (!dataListForecast.isEmpty()) {
                                    Data maxData = Collections.max(dataListForecast, Comparator.comparing(Data::getValue));
                                    Data minData = Collections.min(dataListForecast, Comparator.comparing(Data::getValue));
                                    double sum = dataListForecast.stream()
                                            .mapToDouble(Data::getValue)
                                            .sum();
                                    Double average = sum / dataListForecast.size();
                                    chartData.setDatas(dataListForecast);
                                    chartData.setMaxData(maxData);
                                    chartData.setMinData(minData);
                                    chartData.setAverage(Double.valueOf(df.format(average)));
                                    chartData.setOrder(1);
                                    if (chartData.getMaxData() != null) {
                                        if (max == null) {
                                            max = chartData.getMaxData().getValue();
                                        } else {
                                            if (max < chartData.getMaxData().getValue()) {
                                                max = chartData.getMaxData().getValue();
                                            }
                                        }
                                    }
                                    if (chartData.getMinData() != null) {
                                        if (min == null) {
                                            min = chartData.getMinData().getValue();
                                        } else {
                                            if (min > chartData.getMinData().getValue()) {
                                                min = chartData.getMinData().getValue();
                                            }
                                        }
                                    }
                                    String y1;
                                    if (chartData.getMaxData() != null && chartData.getMinData() != null) {
                                        if (min < 0) {
                                            y1 = "-" + Math.round(Math.abs(min) + 0.5);
                                        } else {
                                            y1 = String.valueOf(Math.round(min));
                                        }
                                        y = y1 + "," + Math.round((max * 1.5));
                                        calResult2.setAxisY(y);
                                    }
                                }
                            }
                            break;
                        case "特征潮位相应增水":
                            chartData.setSensorType("zs");
                            List<Data> dataListFeature = new ArrayList<>();
                            for (BDMS_ST_ASTROTD_F bdmsStAstrotdF : bdmsStAstrotdFs) {

                                dataListForecast = getDataList(values, FORECAST,update);
                                Data data1 = dataListForecast.stream()
                                        .filter(data -> data.getTM().substring(0, 19)
                                                .equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                        .findFirst()
                                        .orElse(null);
                                if (data1 == null) {
                                    String old = bdmsStAstrotdF.getYmdh();
                                    Date newda = format.parse(bdmsStAstrotdF.getYmdh());
                                    int minutes = newda.getMinutes();
                                    newda.setMinutes(0);
                                    bdmsStAstrotdF.setYmdh(format.format(newda));

                                    Optional<Data> prevDataOptional = dataListForecast.stream()
                                            .filter(data -> data.getTM().substring(0, 19).equals(bdmsStAstrotdF.getYmdh().substring(0, 19)))
                                            .findFirst();
                                    // System.out.println(prevDataOptional);
                                    if (prevDataOptional.isPresent()) {
                                        calendar.setTime(format.parse(prevDataOptional.get().getTM()));
                                        calendar.add(Calendar.HOUR, +1);
                                        // System.out.println(format.format(calendar.getTime()));
                                        Optional<Data> nextDataOptional = dataListForecast.stream()
                                                .filter(data -> data.getTM().substring(0, 19).equals(format.format(calendar.getTime())))
                                                .findFirst();
                                        // System.out.println(nextDataOptional);
                                        prevDataOptional.ifPresent(prevData -> {
                                            nextDataOptional.ifPresent(nextData -> {

                                                double averageValue = Double.parseDouble(df.format(prevData.getValue() + ((nextData.getValue() - prevData.getValue()) / 60) * minutes));
                                                Data data2 = new Data();
                                                data2.setTM(old);
                                                data2.setHltdmk(bdmsStAstrotdF.getHltdmk());
                                                data2.setTwc(bdmsStAstrotdF.getFtdz());
                                                data2.setZs(averageValue);
                                                dataListFeature.add(data2);
                                            });
                                        });
                                        bdmsStAstrotdF.setYmdh(old);
                                    }
                                }
                            }
                            chartData.setOrder(3);
                            chartData.setDatas(dataListFeature);
                            break;
                        default:
                            break;
                    }
                    results.add(chartData);
                }
                if (results.size()>2) {
                    List<Data>  dataList =results.get(2).getDatas().stream().filter(data -> data.getZs()!=null).collect(Collectors.toList());
                    for (Data data : dataList) {
                        data.setValue(data.getZs());
                        data.setTwc(null);
                        data.setZs(null);
                        results.get(0).getDatas().add(data);
                    }


                    if (results.get(0).getDatas()!=null&&results.get(0).getDatas().size()>0){
                        results.get(0).getDatas().sort(Comparator.comparing(Data::getTM));
                    }
                    if (results.get(1).getMaxData()!=null){
                        if (max==null){
                            max =results.get(1).getMaxData().getValue();
                        }else {
                            if (max<results.get(1).getMaxData().getValue()){
                                max =results.get(1).getMaxData().getValue();
                            }
                        }

                    }
                    if (results.get(1).getMinData()!=null){
                        if (min==null){
                            min =results.get(1).getMinData().getValue();
                        }else {
                            if (min>results.get(1).getMinData().getValue()){
                                min =results.get(1).getMinData().getValue();
                            }
                        }
                    }
                    String y1;
                    if (results.get(1).getMaxData()!=null&&results.get(1).getMinData()!=null){

                        if (min<0){

                            y1 ="-"+Math.round(Math.abs(min)+0.5);
                        }else {
                            y1= String.valueOf(Math.round(min));
                        }
                        y= y1+","+Math.round((max*1.5));

                        calResult2.setAxisY(y);
                    }

                }

                results.sort(Comparator.comparing(ChartData::getGroupOrder)
                        .thenComparing(ChartData::getOrder));
                results = results.stream()
                        .filter(chartData -> chartData.getDatas() != null && chartData.getDatas().size() > 0)
                        .collect(Collectors.toList());
                calResult2.setResults(results);
                String y1;
                if (max != null && min != null) {
                    if (min < 0) {
                        y1 = "-" + Math.round(Math.abs(min) + 0.5);
                    } else {
                        y1 = String.valueOf(Math.round(min));
                    }
                    System.out.printf("%.2f%n",max * 1.5);
                    if (calResult2.getAlarmValue()!=null) {
                        System.out.printf("%.2f%n", calResult2.getAlarmValue());

                        if (max * 1.5 > calResult2.getAlarmValue()) {
                            max = max + 1.0;
                        }
                    }
                    y = y1 + "," + Math.round((max * 1.5));
                    calResult2.setAxisY(y);
                }else {
                    calResult2.setAxisY("-2,6");
                }
                calResult2.setBaseDatas(baseDatas);
                // calSchemeInfos.add(r);

                calResults.add(calResult2);
            }
        }
        return calResults;
    }





    public void DATA_All(String value) {
        if (values == null) {
            values = new ArrayList<>();
        } else {
            values.clear();
        }
        this.data_all = value;
        if (value == null) {
            return;
        }
        int version = 1;
        int pointIndex = value.indexOf(".");
        if (pointIndex > 0 && value.startsWith("v")) {
            version = Integer.parseInt(value.substring(1, pointIndex - 1));
        }
        if (version == 1) {
            int valueItemLength;
            //System.out.println("value.length():"+value.length());
            for (int i = 0; i < value.length(); i += valueItemLength) {
                // System.out.println("value.charAt(i):"+value.charAt(i));
                valueItemLength = value.charAt(i) == '-' ? 4 : 3;
                if (valueItemLength + i > value.length()) {
                    //System.out.println("tiaochu");
                    continue;
                } else {
                    if (!value.substring(i, valueItemLength + i).isEmpty()) {
                        //  System.out.println(i);
                        //   String str=value.substring(i, valueItemLength+i);
                        //    System.out.println("value.substring(i, valueItemLength)"+str);
                        double var = Double.parseDouble(value.substring(i, valueItemLength + i));
                        // System.out.println("var:"+var);
                        values.add(var / 100);
                    }

                }
            }
        } else if (version == 2) {
            String datavalue = value.substring(pointIndex + 1);
            String[] data0 = datavalue.split(String.valueOf("+"));
            for (int i = 0; i < data0.length; i++) {
                if (isWhitespace(data0[i])) {
                    continue;
                }
                String[] data1 = data0[i].split(String.valueOf("+"));
                for (int j = 0; j < data1.length; j++) {
                    if (isWhitespace(data1[j])) {
                        continue;
                    }
                    if (j == 0) {
                        values.add(Double.parseDouble(data1[0]));
                    } else {
                        values.add(Double.parseDouble(data1[j]) * -1);
                    }
                }
            }
        }
    }
    //获取站点天文潮位
    public List<StationForeCastTideResult> getStationForecastTide(Integer projectId,String startTime,String endTime ,List<String> stationCodes,Integer stepLength,String modelId,Integer DBType) throws ParseException {
        List<StationForeCastTideResult> calResults = new ArrayList<>();
        //获取天文潮
        //根据开始时间结束时间站码集合 去库里查询对应的天文潮数据
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs= bdms_st_astrotd_fService.selectFTDZ(stationCodes,startTime,endTime,0);
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs =  bdms_st_sensor_bService.selectByStcds(stationCodes);
        //判断新库老库
        if (DBType==1){
            BDMS_FBC_RESULT_SCHEME bdms_fbc_result_scheme= bdms_fbc_result_schemeService.selectNewOneByModelId(Integer.parseInt(modelId),startTime,endTime);
            List<BDMS_FBC_RESULT> bdms_fbc_results = null;
            if (bdms_fbc_result_scheme!=null){
                bdms_fbc_results = bdms_fbc_resultService.selecTBySchemeId(bdms_fbc_result_scheme.getSchemeId(), stationCodes);
            }
            Double maxValue = null;
            Double minValue = null;
            //循环站码
            for (String stationCode:stationCodes){
                StationForeCastTideResult stationForeCastTideResult =new StationForeCastTideResult();
                List<ChartData> chartDatas = new ArrayList<>();
                ChartData chartData = new ChartData();
                chartData.setUnit("m");
                chartData.setSensorType("tdz");
                chartData.setValueName("天文潮");
                List<Data> dataList = new ArrayList<>();
                stationForeCastTideResult.setStationCode(stationCode);
                //去站点表获取站名
                BDMS_ST_SENSOR_B bdmsStStbprpB= bdms_st_sensor_bs.stream().filter(bdmsStSensorB -> bdmsStSensorB.getStcd().equals(stationCode)).findAny().orElse(null);
                if (bdmsStStbprpB!=null){
                    stationForeCastTideResult.setStationName(bdmsStStbprpB.getStnm());
                }
                stationForeCastTideResult.setAxisX(startTime+","+endTime);
                stationForeCastTideResult.setAxisY("");
                //设置天文潮数据
                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList =  bdmsStAstrotdFs.stream().filter(bdms_st_astrotd_f -> bdms_st_astrotd_f.getStcd().equals(stationCode)).collect(Collectors.toList());
                for (int i = 0; i < bdmsStAstrotdFList.size() ; i++) {
                    Data data = new Data();
                    data.setTM(bdmsStAstrotdFList.get(i).getYmdh().substring(0,19));
//                    System.out.println(bdmsStAstrotdFList.get(i));
                    if (i<bdmsStAstrotdFList.size()-1){
                        BDMS_ST_ASTROTD_F current = bdmsStAstrotdFList.get(i);
                        BDMS_ST_ASTROTD_F next = bdmsStAstrotdFList.get(i + 1);
                        //D
                        List<Data> datas1 =  arithmeticUtil.genInterpolation(current.getYmdh().substring(0,19),next.getYmdh().substring(0,19),
                                Double.valueOf(df.format(current.getFtdz())),Double.valueOf(df.format(next.getFtdz().doubleValue())),
                                stepLength,"twc");
                        dataList.addAll(datas1);
                    }


                    if (bdmsStAstrotdFList.get(i).getFtdz()<= -99){
                        data.setValue( Double.valueOf(df.format(0.00)));
                    }else {
                        data.setValue(Double.valueOf(df.format(bdmsStAstrotdFList.get(i).getFtdz())));
                    }

                    dataList.add(data);
                }
                dataList.sort(Comparator.comparing(Data::getTM));
                chartData.setDatas(dataList);
                // 设置该站天文潮统计值
                chartData.setMaxData( dataList.stream()
                        .max(Comparator.comparing(Data::getValue))
                        .orElse(null));
                chartData.setMinData( dataList.stream()
                        .min(Comparator.comparing(Data::getValue))
                        .orElse(null));
                chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));

                String y1;
                String y = null;
                if (chartData.getMaxData()!=null&&chartData.getMinData()!=null){
                    maxValue = Double.parseDouble(df.format(chartData.getMaxData().getValue()));
                    minValue = Double.parseDouble(df.format(chartData.getMinData().getValue()));
                    if (minValue<0){
                        y1 ="-"+Math.round(Math.abs(minValue)+0.5);
                    }else {
                        y1= String.valueOf(Math.round(minValue));
                    }
                    y= y1+","+Math.round((maxValue*1.5));
                    stationForeCastTideResult.setMaxValueUpper(maxValue+0.1);
                    stationForeCastTideResult.setMaxVauleLower(maxValue-0.1);
//                    if (minValue<0){
//                        y1 ="-"+Math.round(Math.abs(minValue)+0.5);
//                    }else {
//                        y1= String.valueOf(Math.round(minValue));
//                    }
//                    y= y1+","+Math.round((maxValue*1.5));

                }
                chartData.setGroupOrder(1);
                chartData.setOrder(1);
                chartDatas.add(chartData);
                //判断方案信息 if方案信息不为空姐返回增水和预报潮位

                if (bdms_fbc_results!=null) {
                    Date s1 =format.parse(startTime);
                    Date e1 =format.parse(endTime);
                    Date s2 =format.parse(bdms_fbc_result_scheme.getYmdhm());
                    calendar.setTime(s2);
                    calendar.add(Calendar.HOUR,bdms_fbc_result_scheme.getCalnum());
                    Date e2 =calendar.getTime();

                    boolean hasIntersection = s1.before(e2) && e1.after(s2);
                    if (hasIntersection){
                        stationForeCastTideResult.setHasForecastResults(true);
                        CalSchemeInfo calSchemeInfo = new CalSchemeInfo();
                        calSchemeInfo.setCalSchemeId(String.valueOf(bdms_fbc_result_scheme.getSchemeId()));
                        calSchemeInfo.setTyphoonId(String.valueOf(bdms_fbc_result_scheme.getTfid()));
                        calSchemeInfo.setForecastTime(bdms_fbc_result_scheme.getYmdhm());
                        try {
                            Date ForecastEndTM = format.parse(bdms_fbc_result_scheme.getYmdhm());
                            calendar.setTime(ForecastEndTM);
                            calendar.add(Calendar.HOUR, +bdms_fbc_result_scheme.getCalnum());
                            calSchemeInfo.setForecastEndTime(format.format(calendar.getTime()));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        calSchemeInfo.setUserId(String.valueOf(bdms_fbc_result_scheme.getUserId()));
                        calSchemeInfo.setModelId(String.valueOf(bdms_fbc_result_scheme.getModelId()));
                        calSchemeInfo.setTyphoonId(String.valueOf(bdms_fbc_result_scheme.getTfid()));
                        calSchemeInfo.setTyphoonDepart(bdms_fbc_result_scheme.getReportDept());
                        calSchemeInfo.setState(String.valueOf(bdms_fbc_result_scheme.getState()));
                        calSchemeInfo.setModelPar(String.valueOf(bdms_fbc_result_scheme.getExpparam()));
                        calSchemeInfo.setRemark("state 0 新建方案、1 计算中、2 计算失败、3 计算成功");
                        // calSchemeInfo.setRemark(bdms_fbc_result_scheme.getFlag());
                        calSchemeInfo.setUserName("预报员");
                        ChartData zsC = new ChartData();
                        ChartData ftC = new ChartData();
                        zsC.setUnit("m");
                        ftC.setUnit("m");
                        zsC.setGroupOrder(2);
                        zsC.setOrder(1);
                        ftC.setGroupOrder(2);
                        ftC.setOrder(2);
                        zsC.setSensorType("zs");
                        zsC.setValueName("预报增水");
                        ftC.setSensorType("ftdz");
                        ftC.setValueName("预报潮位");
                        //生成chardata zs和ftdz
                        List<Data> ftdzDatas = new ArrayList<>();
                        List<Data> zsDatas = new ArrayList<>();
                        //根据方案的预报时间和计算时段 构造数据的开始与结束时间
                        String fortcastTime = bdms_fbc_result_scheme.getYmdhm();
                        //计算时段数
                        int calNum = bdms_fbc_result_scheme.getCalnum();
                        //预报结束时间 预报时间加计算时段数
                        try {
                            calendar.setTime(format.parse(fortcastTime));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        calendar.add(Calendar.HOUR, +calNum);
                        String forecastEndTime = format.format(calendar.getTime());
                        //根据开始时间结束时间获取增水数据
                        List<BDMS_FBC_RESULT> bdms_fbc_resultsList = bdms_fbc_results.stream().filter(BDMS_FBC_RESULT -> BDMS_FBC_RESULT.getStcd().equals(stationCode)).collect(Collectors.toList());
                        //获取值`xedcrtv fty
                        for (BDMS_FBC_RESULT bdms_fbc_result : bdms_fbc_resultsList) {
                            DATA_All(bdms_fbc_result.getDataAll());
                        }
                        //获取values平均值
                        if (values != null) {
                            for (int i = 0; i < values.size(); i++) {
                                try {
                                    Date date = format.parse(fortcastTime);
                                    calendar.setTime(date);
                                    calendar.add(Calendar.HOUR, +i);
                                    zsDatas.add(new Data(format.format(calendar.getTime()), values.get(i)));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        zsDatas.sort(Comparator.comparing(Data::getTM));
                        for (Data zsdata : zsDatas) {
                            Double twc = null;
                            Data da = dataList.stream().filter(data -> data.getTM().substring(0, 19).equals(zsdata.getTM().substring(0, 19))).findAny().orElse(null);
                            if (da != null) {
                                Data fData = new Data();
                                twc = da.getValue();
                                fData.setTM(zsdata.getTM().substring(0, 19));
                                fData.setValue(Double.valueOf(df.format((twc + zsdata.getValue()))));
                                ftdzDatas.add(fData);
                            }


                        }
                        zsC.setMaxData(zsDatas.stream()
                                .max(Comparator.comparing(Data::getValue))
                                .orElse(null));
                        ftC.setMinData(ftdzDatas.stream()
                                .min(Comparator.comparing(Data::getValue))
                                .orElse(null));
                        ftC.setMaxData(ftdzDatas.stream()
                                .max(Comparator.comparing(Data::getValue))
                                .orElse(null));
                        zsC.setMinData(zsDatas.stream()
                                .min(Comparator.comparing(Data::getValue))
                                .orElse(null));
                        ftC.setAverage(Double.valueOf(df.format(ftdzDatas.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                        zsC.setAverage(Double.valueOf(df.format(zsDatas.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                        zsC.setDatas(zsDatas);
                        ftC.setDatas(ftdzDatas);
                        chartDatas.add(ftC);
                        chartDatas.add(zsC);
                        stationForeCastTideResult.setCalSchemeInfo(calSchemeInfo);
                        if (maxValue != null) {
                            if ( maxValue < ftC.getMaxData().getValue()) {
                                maxValue = Double.parseDouble(df.format(ftC.getMaxData().getValue()));
                            }
                            if ( minValue > ftC.getMinData().getValue()) {
                                minValue = Double.parseDouble(df.format(ftC.getMinData().getValue()));
                            }

                            if (minValue < 0) {
                                y1 = "-" + Math.round(Math.abs(minValue) + 0.5);
                            } else {
                                y1 = String.valueOf(Math.round(minValue));
                            }
                            y = y1 + "," + Math.round((maxValue * 1.5));
                            stationForeCastTideResult.setMaxValueUpper(maxValue+0.1);
                            stationForeCastTideResult.setMaxVauleLower(maxValue-0.1);
                        }

                        stationForeCastTideResult.setAxisY(y);
                    }
                }

                stationForeCastTideResult.setDataSet(chartDatas);
                //获取这个站数据下的最大值最小值来构建轴
                chartDatas.sort(Comparator.comparing(ChartData::getGroupOrder)
                        .thenComparing(ChartData::getOrder));

                calResults.add(stationForeCastTideResult);
            }
        }else if(DBType==0){
            BDMS_CALCULATESCHEME_M1 bdmsCalculatescheme = bdms_calculatescheme_mService.selectNewOneByModelMySql(modelId,startTime,endTime);
            List<BDMS_FBC_RESULT_P> bdms_fbc_results = null;
            if (bdmsCalculatescheme!=null){
                bdms_fbc_results = bdms_fbc_result_pService.selecTBySchemeId(bdmsCalculatescheme.getCschemeid(), stationCodes);
            }
            Double maxValue = null;
            Double minValue = null;
            //循环站码
            for (String stationCode:stationCodes){
                StationForeCastTideResult stationForeCastTideResult =new StationForeCastTideResult();
                List<ChartData> chartDatas = new ArrayList<>();
                ChartData chartData = new ChartData();
                chartData.setUnit("m");
                chartData.setSensorType("tdz");
                chartData.setValueName("天文潮");
                List<Data> dataList = new ArrayList<>();
                stationForeCastTideResult.setStationCode(stationCode);
                //去站点表获取站名
                BDMS_ST_SENSOR_B bdmsStStbprpB= bdms_st_sensor_bs.stream().filter(bdmsStSensorB -> bdmsStSensorB.getStcd().equals(stationCode)).findAny().orElse(null);
                if (bdmsStStbprpB!=null){
                    stationForeCastTideResult.setStationName(bdmsStStbprpB.getStnm());
                }
                stationForeCastTideResult.setAxisX(startTime+","+endTime);
                stationForeCastTideResult.setAxisY("");
                //设置天文潮数据
                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList =  bdmsStAstrotdFs.stream().filter(bdms_st_astrotd_f -> bdms_st_astrotd_f.getStcd().equals(stationCode)).collect(Collectors.toList());
                for (int i = 0; i < bdmsStAstrotdFList.size() ; i++) {
                    Data data = new Data();
                    data.setTM(bdmsStAstrotdFList.get(i).getYmdh().substring(0,19));

                    if (i<bdmsStAstrotdFList.size()-1){
                        BDMS_ST_ASTROTD_F current = bdmsStAstrotdFList.get(i);
                        BDMS_ST_ASTROTD_F next = bdmsStAstrotdFList.get(i + 1);
                        //D
                        List<Data> datas1 =  arithmeticUtil.genInterpolation(current.getYmdh().substring(0,19),next.getYmdh().substring(0,19),
                                Double.valueOf(df.format(current.getFtdz())),Double.valueOf(df.format(next.getFtdz().doubleValue())),
                                stepLength,"twc");
                        dataList.addAll(datas1);
                    }


                    if (bdmsStAstrotdFList.get(i).getFtdz()<= -99){
                        data.setValue( Double.valueOf(df.format(0.00)));
                    }else {
                        data.setValue(Double.valueOf(df.format(bdmsStAstrotdFList.get(i).getFtdz())));
                    }

                    dataList.add(data);
                }
                dataList.sort(Comparator.comparing(Data::getTM));
                chartData.setDatas(dataList);
                // 设置该站天文潮统计值
                chartData.setMaxData( dataList.stream()
                        .max(Comparator.comparing(Data::getValue))
                        .orElse(null));
                chartData.setMinData( dataList.stream()
                        .min(Comparator.comparing(Data::getValue))
                        .orElse(null));
                chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));

                String y1 = null;
                String y = null;
                if (chartData.getMaxData()!=null&&chartData.getMinData()!=null){
                    maxValue = Double.parseDouble(df.format(chartData.getMaxData().getValue()));
                    minValue = Double.parseDouble(df.format(chartData.getMinData().getValue()));
                    if (minValue<0){
                        y1 ="-"+Math.round(Math.abs(minValue)+0.5);
                    }else {
                        y1= String.valueOf(Math.round(minValue));
                    }
                    y= y1+","+Math.round((maxValue*1.5));
                    stationForeCastTideResult.setMaxValueUpper(maxValue+0.1);
                    stationForeCastTideResult.setMaxVauleLower(maxValue-0.1);
                    stationForeCastTideResult.setAxisY(y);
                }
                chartData.setGroupOrder(1);
                chartData.setOrder(1);
                chartDatas.add(chartData);
                //判断方案信息 if方案信息不为空姐返回增水和预报潮位

//                System.out.println( startTime);
//                System.out.println( endTime);
//                System.out.println( bdmsCalculatescheme.getDtmforecast());
//                System.out.println( bdmsCalculatescheme.getDtmend());
//
//                System.out.println(s1.before(e1));
//                System.out.println(e1.before(s2));
//                System.out.println(s1.before(e2));
//                System.out.println(e2.before(s2));
                if (bdmsCalculatescheme!=null){
                    Date s1 =format.parse(startTime);
                    Date e1 =format.parse(endTime);
                    Date s2 =format.parse(bdmsCalculatescheme.getDtmforecast());
                    Date e2 =format.parse(bdmsCalculatescheme.getDtmend());
                    boolean hasIntersection = s1.before(e2) && e1.after(s2);
                    if (bdms_fbc_results!= null && hasIntersection) {
                        stationForeCastTideResult.setHasForecastResults(true);
                        CalSchemeInfo calSchemeInfo = new CalSchemeInfo();
                        //设置方案基础信息
                        calSchemeInfo.setCalSchemeId(bdmsCalculatescheme.getCschemeid());
                        calSchemeInfo.setCalSchemeName(bdmsCalculatescheme.getCschemenm());
                        calSchemeInfo.setUserId(bdmsCalculatescheme.getUserid());
                        calSchemeInfo.setUserName("预报员");
                        calSchemeInfo.setForecastTime(bdmsCalculatescheme.getDtmforecast());
                        calSchemeInfo.setForecastEndTime(bdmsCalculatescheme.getDtmend());
                        calSchemeInfo.setForecastOperateTime(bdmsCalculatescheme.getDtmcalculate());
                        String param = bdmsCalculatescheme.getParams();
                        String[] params = param.split(",");
                        String typhoonId = params[0];
                        String dept= params[1];
                        String xs =df.format(Double.parseDouble(params[2]));
                        BDMS_FBC_TFINFO_B bdmsFbcTfinfoB = bdms_fbc_tfinfo_bService.queryById(String.valueOf(typhoonId));
                        calSchemeInfo.setTyphoonId(typhoonId);
                        calSchemeInfo.setTyphoonName(bdmsFbcTfinfoB.getTfnameC());
                        calSchemeInfo.setModelPar(xs);
                        calSchemeInfo.setTyphoonDepart(dept);
                        BDMS_FBC_OCEANMODEL_B bdmsFbcOceanmodelB = bdms_fbc_oceanmodel_bService.selectByModelId(projectId,bdmsCalculatescheme.getMdid());
                        calSchemeInfo.setModelId(bdmsFbcOceanmodelB.getModelid());
                        String modelName = bdmsFbcOceanmodelB.getModelnm();
                        calSchemeInfo.setModelName(modelName);
                        calSchemeInfo.setModelAbb(bdmsFbcOceanmodelB.getModelabb());
                        calSchemeInfo.setFlag(bdmsCalculatescheme.getFlag());
                        calSchemeInfo.setHasArea(bdmsCalculatescheme.getIsarea());
                        calSchemeInfo.setHasGen(bdmsCalculatescheme.getIsgen());
                        calSchemeInfo.setState(bdmsCalculatescheme.getState());
                        calSchemeInfo.setRemark("state 0 新建方案、1 计算中、2 计算失败、3 计算成功");
                        ChartData zsC = new ChartData();
                        ChartData ftC = new ChartData();
                        zsC.setUnit("m");
                        ftC.setUnit("m");
                        zsC.setGroupOrder(2);
                        zsC.setOrder(1);
                        ftC.setGroupOrder(2);
                        ftC.setOrder(2);
                        zsC.setSensorType("zs");
                        zsC.setValueName("预报增水");
                        ftC.setSensorType("ftdz");
                        ftC.setValueName("预报潮位");
                        //生成chardata zs和ftdz
                        List<Data> ftdzDatas = new ArrayList<>();
                        List<Data> zsDatas = new ArrayList<>();
                        //根据方案的预报时间和计算时段 构造数据的开始与结束时间


                        //根据开始时间结束时间获取增水数据
                        List<BDMS_FBC_RESULT_P> bdms_fbc_resultsList = bdms_fbc_results.stream().filter(BDMS_FBC_RESULT -> BDMS_FBC_RESULT.getPtkey().equals(stationCode)).collect(Collectors.toList());
                        //获取值`xedcrtv fty
                        double update =0;
                        for (BDMS_FBC_RESULT_P bdms_fbc_result : bdms_fbc_resultsList) {
                            if (bdms_fbc_result!=null){
                                data_all(bdms_fbc_result.getDtvalues());
                                if (bdms_fbc_result.getRemark()!=null && !"".equals(bdms_fbc_result.getRemark())){
                                    update = Double.parseDouble(bdms_fbc_result.getRemark());
                                }
                                //获取values平均值
                                if (values != null) {
                                    for (int i = 0; i < values.size(); i++) {
                                        try {
                                            Date date = format.parse(bdmsCalculatescheme.getDtmforecast());
                                            calendar.setTime(date);
                                            calendar.add(Calendar.HOUR, +i);
                                            zsDatas.add(new Data(format.format(calendar.getTime()), values.get(i)+update));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }


                                zsDatas.sort(Comparator.comparing(Data::getTM));
                            }
                        }
                        if (zsDatas.size()>0){
                            for (Data zsdata : zsDatas) {
                                Data da = dataList.stream().filter(data -> data.getTM().substring(0, 19).equals(zsdata.getTM().substring(0, 19))).findAny().orElse(null);
                                if (da != null) {
                                    Data fData = new Data();
                                    Double  twc = da.getValue();
                                    fData.setTM(zsdata.getTM().substring(0, 19));
                                    fData.setValue(Double.valueOf(df.format((twc + zsdata.getValue()))));
                                    ftdzDatas.add(fData);
                                }
                            }
                            zsC.setMaxData(zsDatas.stream()
                                    .max(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            zsC.setMinData(zsDatas.stream()
                                    .min(Comparator.comparing(Data::getValue))
                                    .orElse(null));
                            zsC.setAverage(Double.valueOf(df.format(zsDatas.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                            zsC.setDatas(zsDatas);
                        }


                        ftC.setMinData(ftdzDatas.stream()
                                .min(Comparator.comparing(Data::getValue))
                                .orElse(null));
                        ftC.setMaxData(ftdzDatas.stream()
                                .max(Comparator.comparing(Data::getValue))
                                .orElse(null));

                        ftC.setAverage(Double.valueOf(df.format(ftdzDatas.stream().collect(Collectors.averagingDouble(Data::getValue)))));

                        ftC.setDatas(ftdzDatas);
                        chartDatas.add(ftC);
                        chartDatas.add(zsC);
                        // results.add(zsC);
                        // results.add(ftC);
                        //calSchemeInfo.setResults(results);

                        stationForeCastTideResult.setCalSchemeInfo(calSchemeInfo);
                        if (ftC.getMaxData() != null && ftC.getMinData() != null) {
                            if (maxValue != null) {
                                if ( maxValue < ftC.getMaxData().getValue()) {
                                    maxValue = Double.parseDouble(df.format(ftC.getMaxData().getValue()));
                                }
                                if ( minValue > ftC.getMinData().getValue()) {
                                    minValue = Double.parseDouble(df.format(ftC.getMinData().getValue()));
                                }

                                if (minValue < 0) {
                                    y1 = "-" + Math.round(Math.abs(minValue) + 0.5);
                                } else {
                                    y1 = String.valueOf(Math.round(minValue));
                                }
                                y = y1 + "," + Math.round((maxValue * 1.5));
                                stationForeCastTideResult.setMaxValueUpper(maxValue+0.1);
                                stationForeCastTideResult.setMaxVauleLower(maxValue-0.1);
                            }

                        }

                        stationForeCastTideResult.setAxisY(y);
                    }
                }
                stationForeCastTideResult.setDataSet(chartDatas);
                //获取这个站数据下的最大值最小值来构建轴
                chartDatas.sort(Comparator.comparing(ChartData::getGroupOrder)
                        .thenComparing(ChartData::getOrder));

                calResults.add(stationForeCastTideResult);
            }

        }





        return calResults;
    }


    public List<CalResult> getDataByCalSchemeIdStationcodes(Integer pid, List<String> calSchemeIds, List<String> stationCodes, int twcDept, int backDays, Integer stepLength,Integer DBType) throws IOException, ParseException {

        List<CalResult> calResults = new ArrayList<>();
        List<CalSchemeInfo> calSchemeInfos = new ArrayList<>();


        double minV = 0.0;
        double maxV;
        //获取所有站点的警戒值
        List<BDMS_ST_SENSORALARM_B> bdms_st_sensoralarm_bs= bdms_st_sensoralarm_bService.selectByStcd2(stationCodes);
        //获取实测取数码
        List<String> Stcds = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b : bdms_st_sensor_bs) {
            Stcds.add(bdms_st_sensor_b.getDatacode());
        }

        String minStartTime = null;
        String maxEndTime = null;
        //当dbtype =0 huozhenull 老库查询
        if (DBType==1){
            int res =0;
            for (String calSchemeId : calSchemeIds) {

                //构造方案信息对象 包含返回结构
                CalSchemeInfo calSchemeInfo = new CalSchemeInfo();
                //结果对象
                //List<ChartData> results = new ArrayList<>();
                res+=1;
                //获取计算方案
                List<BDMS_FBC_RESULT_SCHEME> bdms_fbc_result_schemes = bdms_fbc_result_schemeService.selectByPid(pid, Integer.parseInt(calSchemeId));
                BDMS_FBC_RESULT_SCHEME bdms_fbc_result_scheme = bdms_fbc_result_schemes.get(0);
                //设置方案基础信息
                calSchemeInfo.setCalSchemeId(String.valueOf(bdms_fbc_result_scheme.getSchemeId()));
                calSchemeInfo.setTyphoonId(String.valueOf(bdms_fbc_result_scheme.getTfid()));
                calSchemeInfo.setForecastTime(bdms_fbc_result_scheme.getYmdhm());
                try {
                    Date ForecastEndTM = format.parse(bdms_fbc_result_scheme.getYmdhm());
                    calendar.setTime(ForecastEndTM);
                    calendar.add(Calendar.HOUR,+bdms_fbc_result_scheme.getCalnum());
                    calSchemeInfo.setForecastEndTime(format.format(calendar.getTime()));
                }catch (Exception e){
                    e.printStackTrace();
                }
                calSchemeInfo.setUserId(String.valueOf(bdms_fbc_result_scheme.getUserId()));
                calSchemeInfo.setModelId(String.valueOf(bdms_fbc_result_scheme.getModelId()));
                calSchemeInfo.setTyphoonId(String.valueOf(bdms_fbc_result_scheme.getTfid()));
                calSchemeInfo.setTyphoonDepart(bdms_fbc_result_scheme.getReportDept());
                calSchemeInfo.setState(String.valueOf(bdms_fbc_result_scheme.getState()));
                calSchemeInfo.setModelPar(String.valueOf(bdms_fbc_result_scheme.getExpparam()));
                calSchemeInfo.setRemark(bdms_fbc_result_scheme.getFlag());
                calSchemeInfo.setRemark("state 0 新建方案、1 计算中、2 计算失败、3 计算成功");
                calSchemeInfos.add(calSchemeInfo);
                //获取单站方案计算结果
                List<BDMS_FBC_RESULT> bdms_fbc_results = bdms_fbc_resultService.selecTBySchemeId(Integer.parseInt(calSchemeId), stationCodes);
                for (String stationCode : stationCodes) {
                    List<ChartData> results = new ArrayList<>();
                    CalResult calResult2 = new CalResult();
                    List<ChartData> baseDatas = new ArrayList<>();
                    BDMS_ST_SENSORALARM_B bdms_st_sensoralarmb= bdms_st_sensoralarm_bs.stream().filter(bdms_st_sensoralarm_b -> bdms_st_sensoralarm_b.getStcd()
                            .equals(stationCode)).findAny().orElse(null);
                    Double alarmValue = null;
                    if (bdms_st_sensoralarmb != null) {
                        alarmValue = bdms_st_sensoralarmb.getAlarmvalue();
                    }
                    //构造开始时间结束时间
                    //获取预报时间
                    String forecastTime = bdms_fbc_result_scheme.getYmdhm();
                    calendar.setTime(format.parse(forecastTime));
                    calendar.add(Calendar.DATE, -backDays);
                    //预报时间点减去回退天数构造开始时间
                    String startTime = format.format(calendar.getTime());
                    //获取结束时间
                    String endTime = calSchemeInfo.getForecastEndTime();
                    //设置主副标题
                    String stationName = bdms_st_sensor_bService.selectbyStcd(stationCode).getStnm();
                    String mainTitle = stationName+"("+stationCode+")"+"潮位预报综合过程图";
                    //当方案id大于1是只设置作业预报时间
                    String subTitle;
                    if (calSchemeIds.size()==1){
                        subTitle  = "作业预报时间："+forecastTime+" 预报路径："+calSchemeInfo.getTyphoonDepart()+" 预报模型："
                                +calSchemeInfo.getModelName()+" 台风系数："+calSchemeInfo.getModelPar();
                    }else {
                        subTitle  = "作业预报时间："+forecastTime;
                    }
                    //构造xy轴
                    String x = startTime + "," + endTime;
                    String y = null;
                    //警戒值
                    calResult2.setStationCode(stationCode);
                    calResult2.setStationName(stationName);
                    calResult2.setTitle(mainTitle);
                    calResult2.setAlarmValue(alarmValue);
                    calResult2.setSubTitle(subTitle);
                    calResult2.setAxisX(x);
                    //获取预报前后所有的实测数据
                    List<ST_TIDE_R> stTideRs = st_tide_rService.selectByStcd(Stcds,startTime,endTime);
                    //解析实测
                    String datacode = bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B -> BDMS_ST_SENSOR_B.getStcd().equals(stationCode)).findAny().orElse(null).getDatacode();
                    List<ST_TIDE_R> st_tide_rs = stTideRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(datacode) && ST_TIDE_R.getMin().equals("00")).collect(Collectors.toList());
                    //获取预报前的实测
                    ArrayList<ST_TIDE_R> beforeTimeList = new ArrayList<>();
                    //获取预报后的实测
                    ArrayList<ST_TIDE_R> afterTimeList = new ArrayList<>();
                    for (ST_TIDE_R obj : st_tide_rs) {
                        // 获取对象的时间点
                        Date objTime = format.parse(obj.getTm());
                        // 根据时间点与指定时间点进行比较
                        if (objTime.before(format.parse(forecastTime))) {
                            // 添加到指定时间点之前的集合
                            beforeTimeList.add(obj);
                        } else {
                            // 添加到指定时间点之后的集合
                            afterTimeList.add(obj);
                        }
                    }
                    //天文潮
                    //获取天文潮
                    List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList = bdms_st_astrotd_fService.selectFTDZ(stationCodes,startTime,endTime,twcDept);
                    List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdmsStAstrotdFList.stream().filter(BDMS_ST_ASTROTD_F -> BDMS_ST_ASTROTD_F.getStcd().equals(stationCode)).collect(Collectors.toList());
                    List<Data> datas = new ArrayList<>();
                    //解析天文潮位
                    for (BDMS_ST_ASTROTD_F bdmsStAstrotdF:bdmsStAstrotdFs ){
//                    if (bdmsStAstrotdF.getYmdh().substring(14,16).equals("00")){
                        Data data = new Data();
                        data.setTM(bdmsStAstrotdF.getYmdh().substring(0,19));
                        data.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz())));
                        datas.add(data);
                        //  }
                    }
                    //构造基础信息
                    if (res==calSchemeIds.size()){
                        String[] baseValueNames = {"实测潮位-预报前","实测潮位-预报后","天文潮位"};
                        int k=0;
                        for (String valueName : baseValueNames) {
                            ChartData chartData = new ChartData();
                            List<Data> dataList;
                            switch (valueName) {
                                case "实测潮位-预报前":
                                    dataList = getDataListStream(beforeTimeList);
                                    if (dataList.size()>0){
                                        chartData.setSensorType("tdz");
                                        chartData.setMaxData(dataList.stream()
                                                .max(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setMinData(dataList.stream()
                                                .min(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                        chartData.setGroupOrder(1);
                                        chartData.setOrder(1);
                                        chartData.setDatas(dataList);
                                        chartData.setValueName(valueName);
                                        chartData.setUnit("m");
                                        baseDatas.add(chartData);
                                    }

                                    break;
                                case "实测潮位-预报后":
                                    chartData.setSensorType("tdz");
                                    dataList = getDataListStream(afterTimeList);
                                    if (dataList.size()>0) {
                                        chartData.setMaxData(dataList.stream()
                                                .max(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setMinData(dataList.stream()
                                                .min(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                        chartData.setDatas(dataList);
                                        chartData.setGroupOrder(1);
                                        chartData.setOrder(2);
                                        chartData.setValueName(valueName);
                                        chartData.setUnit("m");
                                        baseDatas.add(chartData);
                                    }
                                    break;
                                case "天文潮位":
                                    chartData.setSensorType("twc");
                                    if (datas.size()>0){
                                        dataList = datas;
                                        chartData.setMaxData(dataList.stream()
                                                .max(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setMinData(dataList.stream()
                                                .min(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                        chartData.setDatas(dataList);
                                        chartData.setGroupOrder(2);
                                        chartData.setOrder(1);
                                        chartData.setValueName(valueName);
                                        chartData.setUnit("m");
                                        baseDatas.add(chartData);
                                    }

                                    break;
                                default:
                                    break;
                            }


                        }
                    }
                    baseDatas.sort(Comparator.comparing(ChartData::getGroupOrder)
                            .thenComparing(ChartData::getOrder));
                    //calResult2.setBaseDatas(baseDatas);
                    List<BDMS_FBC_RESULT> bdms_fbc_resultsList = bdms_fbc_results.stream().filter(BDMS_FBC_RESULT -> BDMS_FBC_RESULT.getStcd().equals(stationCode)).collect(Collectors.toList());
                    System.out.println("size"+bdms_fbc_resultsList.size());


                    // System.out.println("bdms_fbc_resultsList"+bdms_fbc_resultsList);
                    if (bdms_fbc_resultsList.size()>0){
                        //获取值`xedcrtv fty
                        String[] valueNames = { "预报增水","预报潮位","特征潮位相应增水"};
                        //  List<BDMS_FBC_RESULT_P> bdms_fbc_resultsList = bdms_fbc_results.stream().filter(BDMS_FBC_RESULT -> BDMS_FBC_RESULT.getPtkey().equals(stationCode)).collect(Collectors.toList());
                        for (String valueName : valueNames) {
                            ChartData chartData = new ChartData();
                            List<Data> dataList ;
                            switch (valueName) {
                                case "预报增水":
                                    chartData.setSensorType("zs");
                                    dataList = new ArrayList<>();
                                    for (BDMS_FBC_RESULT bdms_fbc_result : bdms_fbc_resultsList) {
                                        DATA_All(bdms_fbc_result.getDataAll());
                                    }
                                    //获取values
                                    if (values != null) {
                                        for (int i = 0; i < values.size(); i++) {
                                            try {
                                                Date date = format.parse(forecastTime);
                                                calendar.setTime(date);
                                                calendar.add(Calendar.HOUR, +i);
                                                dataList.add(new Data(format.format(calendar.getTime()), values.get(i)));
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                    dataList.sort(Comparator.comparing(Data::getTM));
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setDatas(dataList);
                                    chartData.setGroupOrder(3);
                                    chartData.setOrder(1);
                                    break;
                                case "预报潮位":
                                    chartData.setSensorType("ftdz");
                                    dataList = new ArrayList<>();
                                    for (BDMS_FBC_RESULT bdms_fbc_result : bdms_fbc_resultsList) {
                                        DATA_All(bdms_fbc_result.getDataAll());
                                    }
                                    //获取values
                                    if (values != null) {
                                        for (int i = 0; i < values.size(); i++) {
                                            try {
                                                Date date = format.parse(forecastTime);
                                                calendar.setTime(date);
                                                calendar.add(Calendar.HOUR, +i);
                                                dataList.add(new Data(format.format(calendar.getTime()), values.get(i)));
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                    dataList.sort(Comparator.comparing(Data::getTM));
                                    List<Data> dataList1 =new ArrayList<>();
                                    for (Data data : getDataList(values, forecastTime,0.0)) {
                                        BDMS_ST_ASTROTD_F bdmsStAstrotdF = bdmsStAstrotdFs.stream().filter(bdms_st_astrotd_f ->
                                                        bdms_st_astrotd_f.getYmdh().substring(0, 19).
                                                                equals(data.getTM().substring(0, 19)))
                                                .findAny().orElse(null);
                                        if (bdmsStAstrotdF != null) {
                                            Data data1 = new Data();
                                            data1.setTM(data.getTM());
                                            data1.setValue(Double.valueOf(df.format(bdmsStAstrotdF.getFtdz() + data.getValue())));
                                            dataList1.add(data1);
                                        }
                                    }
                                    if (dataList1.size()>0){
                                        chartData.setMaxData( dataList1.stream()
                                                .max(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        chartData.setMinData( dataList1.stream()
                                                .min(Comparator.comparing(Data::getValue))
                                                .orElse(null));
                                        double maxValue = Double.parseDouble(df.format(chartData.getMaxData().getValue()));
                                        double minValue = Double.parseDouble(df.format(chartData.getMinData().getValue()));
                                        String y1 = null;
                                        if (chartData.getMaxData()!=null&&chartData.getMinData()!=null){
                                            maxValue = Double.parseDouble(df.format(chartData.getMaxData().getValue()));
                                            minValue = Double.parseDouble(df.format(chartData.getMinData().getValue()));
                                            if (minValue<0){

                                                y1 ="-"+Math.round(Math.abs(minValue)+0.5);
                                            }else {
                                                y1= String.valueOf(Math.round(minValue));
                                            }
                                            y= y1+","+Math.round((maxValue*1.5));
                                        }
                                    }
                                    calResult2.setAxisY(y);
                                    chartData.setAverage(Double.valueOf(df.format(dataList1.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setGroupOrder(3);
                                    chartData.setOrder(2);
                                    chartData.setDatas(dataList1);
                                    break;
                                case "特征潮位相应增水":
                                    chartData.setSensorType("zs");
                                    for (BDMS_FBC_RESULT bdms_fbc_result : bdms_fbc_resultsList) {
                                        DATA_All(bdms_fbc_result.getDataAll());
                                    }
                                    dataList = new ArrayList<>();
                                    for (BDMS_ST_ASTROTD_F bdmsStAstrotdF : bdmsStAstrotdFs) {
                                        Data data1 =  getDataList(values, forecastTime,0.0).stream().filter(data -> data.getTM().substring(0,19)
                                                .equals(bdmsStAstrotdF.getYmdh().substring(0,19))).findAny().orElse(null);
                                        if (data1==null){
                                            String old = bdmsStAstrotdF.getYmdh();
                                            Date newda = format.parse(bdmsStAstrotdF.getYmdh());
                                            int min = newda.getMinutes();
                                            newda.setMinutes(0);
                                            bdmsStAstrotdF.setYmdh(format.format(newda));
                                            Data prevData = null;
                                            Data nextData = null;
                                            for (int k = 0; k < getDataList(values, forecastTime,0.0).size(); k++) {
                                                if (getDataList(values, forecastTime,0.0).get(k).getTM().substring(0,19).equals(bdmsStAstrotdF.getYmdh().substring(0,19))) {
                                                    if (k >= 0) {
                                                        prevData = getDataList(values, forecastTime,0.0).get(k);
                                                    }
                                                    if (k < getDataList(values, forecastTime,0.0).size() - 1) {
                                                        nextData = getDataList(values, forecastTime,0.0).get(k + 1);
                                                    }
                                                    break;
                                                }
                                            }
                                            if (prevData != null && nextData != null) {
                                                double averageValue = Double.parseDouble(df.format( prevData.getValue()+(( nextData.getValue()-prevData.getValue()) /60) *min));
                                                Data data2 = new Data();
                                                data2.setTM(old);
                                                data2.setHltdmk(bdmsStAstrotdF.getHltdmk());
                                                data2.setTwc(bdmsStAstrotdF.getFtdz());
                                                data2.setZs(averageValue);
                                                dataList.add(data2);
                                            }
                                        }
                                    }
                                    chartData.setGroupOrder(3);
                                    chartData.setOrder(3);
                                    chartData.setDatas(dataList);
                                    break;
                                default:
                                    break;
                            }
                            chartData.setValueName(valueName);
                            chartData.setUnit("m");
                            //datas 根据预报结果构造
                            results.add(chartData);
                        }
                    }else if (bdms_fbc_resultsList.size()<=0){
                        System.out.println(22222222);
                        //获取值`xedcrtv fty
                        String[] valueNames = { "预报增水","预报潮位","特征潮位相应增水"};
                        //  List<BDMS_FBC_RESULT_P> bdms_fbc_resultsList = bdms_fbc_results.stream().filter(BDMS_FBC_RESULT -> BDMS_FBC_RESULT.getPtkey().equals(stationCode)).collect(Collectors.toList());
                        for (String valueName : valueNames) {
                            ChartData chartData = new ChartData();
                            List<Data> dataList ;
                            switch (valueName) {
                                case "预报增水":
                                    chartData.setSensorType("zs");
                                    dataList = new ArrayList<>();

                                    //获取values
                                    for (int i = 0; i < bdmsStAstrotdFs.size(); i++) {
                                        try {
                                            Date date = format.parse(forecastTime);
                                            calendar.setTime(date);
                                            calendar.add(Calendar.HOUR, +i);
                                            dataList.add(new Data(format.format(calendar.getTime()), 0.0));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }


                                    dataList.sort(Comparator.comparing(Data::getTM));
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(0.0);
                                    chartData.setDatas(dataList);
                                    chartData.setGroupOrder(3);
                                    chartData.setOrder(1);
                                    break;
                                case "预报潮位":
                                    chartData.setSensorType("ftdz");
                                    dataList = new ArrayList<>();

                                    for (BDMS_ST_ASTROTD_F bdmsStAstrotdF:bdmsStAstrotdFs){



                                        Data data1 = new Data();
                                        data1.setTM(bdmsStAstrotdF.getYmdh());
                                        data1.setValue(bdmsStAstrotdF.getFtdz());
                                        dataList.add(data1);
                                    }



                                    calResult2.setAxisY(y);
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setGroupOrder(3);
                                    chartData.setOrder(2);
                                    chartData.setDatas(dataList);
                                    break;
                                case "特征潮位相应增水":
                                    chartData.setSensorType("zs");

                                    dataList = new ArrayList<>();
                                    for (BDMS_ST_ASTROTD_F bdmsStAstrotdF : bdmsStAstrotdFs) {
                                        if (bdmsStAstrotdF.getHltdmk()!=null && bdmsStAstrotdF.getHltdmk()>0){
                                            Data data = new Data();
                                            data.setTM(bdmsStAstrotdF.getYmdh());
                                            data.setValue(bdmsStAstrotdF.getFtdz());
                                            dataList.add(data);
                                        }
                                    }
                                    chartData.setGroupOrder(3);
                                    chartData.setOrder(3);
                                    chartData.setDatas(dataList);
                                    break;
                                default:
                                    break;
                            }
                            chartData.setValueName(valueName);
                            chartData.setUnit("m");
                            //datas 根据预报结果构造
                            results.add(chartData);
                        }
                    }
                    results.sort(Comparator.comparing(ChartData::getGroupOrder)
                            .thenComparing(ChartData::getOrder));
                    results.removeIf(obj -> obj.getDatas().isEmpty());
                    double maxValue1 = baseDatas.stream()
                            .flatMap(chartData -> chartData.getDatas().stream())
                            .filter(Objects::nonNull)
                            .mapToDouble(Data::getValue)
                            .max()
                            .orElse(Double.MIN_VALUE);
                    double maxValue2 = results.stream()
                            .flatMap(chartData -> chartData.getDatas().stream())
                            .filter(data -> data != null && data.getValue() != null)
                            .mapToDouble(Data::getValue)
                            .max()
                            .orElse(Double.MIN_VALUE);
                    double minValue1 = baseDatas.stream()
                            .flatMap(chartData -> chartData.getDatas().stream())
                            .filter(Objects::nonNull)
                            .mapToDouble(Data::getValue)
                            .min()
                            .orElse(Double.MIN_VALUE);

                    double minValue2 = results.stream()
                            .flatMap(chartData -> chartData.getDatas().stream())
                            .filter(data -> data != null && data.getValue() != null)
                            .mapToDouble(Data::getValue)
                            .min()
                            .orElse(Double.MIN_VALUE);
                    maxV = Math.max(maxValue1, maxValue2);
                    minV = Math.min(minValue1, minValue2);

                    String y1 = null;

                    if (minV < 0) {

                        y1 = "-" + Math.round(Math.abs(minV) + 0.5);
                    } else {
                        y1 = String.valueOf(Math.round(minV));
                    }
                    y = y1 + "," + Math.round((maxV * 1.5));
                    calResult2.setAxisY(y);
                    //calSchemeInfo.setResults(results);
                    calResult2.setResults(results);
                    baseDatas.removeIf(obj -> obj.getDatas().isEmpty());
                    calResult2.setBaseDatas(baseDatas);
                    calResult2.setCalSchemeInfos(calSchemeInfos);
                    calResults.add(calResult2);
                    //results.clear();
                }}

        }



        return calResults;
    }


}